I am using Jline2 framework to build a console application in Java. When launching the application from Eclipse on Windows 7, the shell exists immediately and does not wait for the user to input commands.
Any idea on what might be wrong?
Finally found a small trick to make it work. Add the following VM option to the execution parameters (Run Config/Debug Config)
-Djline.terminal=jline.UnsupportedTerminal
This will make the code run inside Eclipse. However, some strange characters may be written to the console during the course of the run.
Hope this helps.
If you are using picocli with jline2.
You can delegate the heuristic to picocli which seems better than the jline2 heuristic (instead of using -Djline.terminal=jline.UnsupportedTerminal)
The code can looks like :
// JLine 2 does not detect some terminal as not ANSI compatible, like Eclipse Console
// see : https://github.com/jline/jline2/issues/185
// So use picocli heuristic instead :
if (!Help.Ansi.AUTO.enabled() && //
Configuration.getString(TerminalFactory.JLINE_TERMINAL, TerminalFactory.AUTO).toLowerCase()
.equals(TerminalFactory.AUTO)) {
TerminalFactory.configure(TerminalFactory.NONE);
}
(see https://github.com/jline/jline2/issues/185)
Related
I want to run bash file from java, i am using play framework.
any idea how to do that??
is this code enough:
new ProcessBuilder("pathToYourShellScript").start();
Possible duplicate of How to run Unix shell script from Java code? (don't have the reputation to put that in a comment, sorry).
In a nutshell - your line of code will work and the shell script will run by just doing that (tested on a SUSE Linux box).
However, if you want to do anything with the output or to know that it has completed, you'll have to add more code to check its state (check out the Process.waitFor() API) or to capture its output in Java (e.g. by capturing Standard out in a BufferedReader) - check Process.getInputStream() API.
I faced strange problem in Sonar (code analysis tool) and do not understand why it is faile. It is started using command line:
%JAVA_EXEC% %SONAR_OPTIONS% -cp "%SONAR_RUNNER_HOME%\lib\sonar-runner-dist-2.3.jar" "-Drunner.home=%SONAR_RUNNER_HOME%" "-Dproject.home=%PROJECT_HOME%" org.sonar.runner.Main %*
I am not Java programmer, but I would like to debug this process somehow. I know that error appears inside one of loaded plugins, not inside sonar itself.
Question: is there way to start java application with debugging availability without eclipse and any other special and very massive editor? Something command line'ed with ability to set breakpoint, inspect variable value and stepping forward/stepping over is enough for me.
There's JDB, the Java Debugger from the official SDK.
Simply add this parameter to invoked command and connect to this process with prefered debugger (Eclipse, IntelliJ or what you want)
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
You can debug this remotelly!
Setup:
I am working on a Java project which invokes some Perl scripts.
Problem:
If I run the Perl script from terminal, it works fine. But when I invoke the same Perl script with same arguments from Java code, it fails. It is an extremely strange and annoying bug.
I am able to debug Perl in Eclipse using EPIC plugin. And of course, I can debug Java code. It would be helpful to debug Java and Perl code in the same debug session in Eclipse, so that I can see whats different happening when the script is invoked via Java code.
Not sure if this is even possible, but if anyone has any idea, please share.
Thanks.
Perl has some remote debugging capabilities, so you can get what you need, I think. I don't use EPIC, so I don't know if you'll be able to do it all within Eclipse.
Set the environment variable named PERLDB_OPTS to have the value RemotePort=<host>:<port>. Then, start Perl with the -d flag. Instead of the debugger trying to interact with standard IO on the invoking terminal, it will attempt to connection to host:port.
Here's a Unix-y example. I've got a Perl script hello.pm and two terminal windows open.
First terminal
$ nc -l 12345
That's started NetCat as a dumb server listening on port 12345. If you don't have a server listening before Perl starts, it won't work.
Second terminal
$ export PERLDB_OPTS=RemotePort=localhost:12345 # set the env variable
$ perl -d hello.pm
Now the script is running. Back on the first terminal, I see
First terminal
main::(hello.pm:1): print "hello\n";
DB<1>
I'm in the Perl debugger and ready to go!
I know that at least the Komodo IDE has support for remote debugging, and there's a Perl Monks post on doing it with Emacs, so you can get something more than the command line even if it's not Eclipse. Good bug hunting!
If I run the Perl script from terminal, it works fine. But when I invoke the same Perl script with same arguments from Java code, it fails.
I suggest that you check that you are running the same version of Perl in the two "contexts"; i.e. when the Perl app is run from the command line and from Java.
If that's not the problem, check the environment variables.
The other approach to solving this problem would be to focus on why the program is going wrong, not on how / why the contexts are different. Look at the evidence that you have, and look at ways to gather more evidence ...
The potential problem with trying to do this using a Perl a debugger (in an IDE or stand-alone) is that you are actually running Perl in third "context" which might be different from either or both of the existing ones.
Using a debugger may work ... or it may leave you even more confused.
In a project, I'm trying to set up an automated build system for Apache Karaf (there are several commands I need to run in Karaf to set up a working environment on a fresh install). Karaf contains a batch/script file that sets several parameters, and then calls the actual Java program. Essentially, I'd like to be able to do something like:
java MyProgramClass.class < commandTextFile.txt
But when I try this it doesn't do anything. My goal is to simply copy the karaf.bat file, modify it slightly (as below) to make a "karaf-install.bat" that I can just run. The part I've modified of karaf.bat is below, and all I've done is add < "C:\commandFile.txt at the end (the following is all on one line, broken for readability):
"%JAVA%" %JAVA_OPTS% %OPTS% -classpath "%CLASSPATH%"
-Djava.endorsed.dirs="%JAVA_HOME%\jre\lib\endorsed;%JAVA_HOME%\lib\endorsed;%KARAF_HOME%\lib\endorsed"
-Djava.ext.dirs="%JAVA_HOME%\jre\lib\ext;%JAVA_HOME%\lib\ext;%KARAF_HOME%\lib\ext"
-Dkaraf.instances="%KARAF_HOME%\instances" -Dkaraf.home="%KARAF_HOME%"
-Dkaraf.base="%KARAF_BASE%" -Dkaraf.data="%KARAF_DATA%"
-Djava.util.logging.config.file="%KARAF_BASE%\etc\java.util.logging.properties"
%KARAF_OPTS% %MAIN% %ARGS% < "C:\commandFile.txt"
However, Karaf shows nothing. It just runs as if I executed it as normal; my commands are not executed. Is there a way to redirect INTO a java program from the console? Am I doing it way wrong?
For what it's worth, this will eventually be done on both Windows and OS X, but I'm focusing on Windows at the moment.
Update: turns out that this seems to work for me on OS X (Karaf struggles (by saying "Command not found: "), but I think it's because it's getting the commands before it's initialized everything), but Windows is still doesn't even get the commands. I'll poke around more.
When piping INTO, you can read it from System.in.
Consider it a Reader, not an InputStream.
I'm just going to write this issue off as Karaf weirdness, seeing as it works on OS X. I was able to work around it by using the client program that comes with Karaf by doing (on OS X in a .sh file):
"$KARAF/bin/client" "karaf_command_here"
or (on Windows in a .bat file)
call "%KARAF%\bin\client.bat" "karaf_command_here"
And instead of having a list of commands to pipe into Karaf, I just made the list of commands a shell/batch script file that would call Karaf's client for each command. Not as pretty as I'd have liked it, but it got the job done.
(Note you need to start Karaf before using the client with start (and close it with stop)).
When I start a java application, the Java Console is started. But when I start a java application from Eclipse, the Java Console does not start on its own. How can I make it start also when I run an application from Eclipse?
Take a look at this topic .. I think it is exactly what you want.
There is an answer of how to be able to use step-through debugging from Eclipse.
What for do you need java console? There is window console in Eclipse IDE where all System.out.println are redirected.
If you are wanting the program to start in it's own window. Try running it as an external tool. The program would be cmd.exe and the argument would be your java command line, e.g. "java -cp c:\myclasspath myProg" and so forth.
First check that no other console is pinned, icon looks like window with a needle in it. This means that no other console except the one that is pinned will show the output. If everything else fails try to reset the java perspective in Eclipse.