Additional debug option for runnable jar using command prompt - java

I know that the java -jar filename.jar is used for executing the jar file using command prompt, but could anyone please explain the following command:
java -jar filename.jar -debug
What is the exact use of the -debug in the above command?

This is not a flag for the Java Interpreter (as those should be passed before the actual class/jar).
If you execute the command-line you posted above, this will launch your filename.jar-files main class and give it's main(String[] args)-method the -debug-parameter as the second argument in the args-array.
But, there is (or was) also the -debug flag from older Java versions, which has been replaced by the -Xdebug flag. The (certainly old) manpage states that:
-debug Allows the Java debugger - jdb(1) - to attach
itself to this java session. When -debug is
specified on the command line java displays a
password that must be used when starting the
debugging session.
If a Java application is launched with the debug flag, remote-debuggers can attach to the running application and you can (for example) monitor it using JConsole

Related

Launch another JVM from Java with proxy arguments

I'm writting a Java Launcher Program, that should display installed Java programs and launch them, if needed.
Therefore i use the Runtime.getRuntime().exec() method with java.exe -jar myjar.jar as argument.
This works so far, but i also need to pass custom proxy settings to the jvm. From the command line this is possible with:
java.exe -http.proxyHost=www.example.com -http.proxyPort=80 -jar myjar.jar
But if i run this command in java it states:
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
Unrecognized option: -http.proxyHost=www.example.com
So the question: Am i doing something wrong or is this not possible?
Thanks in Advance.
Runtime options should have a D at the beginning:
java.exe -Dhttp.proxyHost=www.example.com -Dhttp.proxyPort=80 -jar myjar.jar

Starting a java swing process on boot using Upstart -- No X11 DISPLAY variable was set

I have a runnable jar file produced from clojure code that I am running on an embedded system (udoo - http://www.udoo.org/), which is running a version of Ubuntu for its OS (udoobuntu - http://www.udoo.org/udoobuntu-the-official-udoo-linux-operating-system/).
The jar file contains a clojure library I wrote, that includes a some Java swing stuff.
Running the jar manually via the command line using:
sudo java -jar myjar.jar
works fine. The sudo is needed for certain usb device permissions.
My problem arises when I try to start the jar using an upstart script called testjob. The relevant part of /etc/init/testjob.conf looks like:
start on (desktop-session-start)
expect fork
script
exec ./home/ubuntu/start > /home/ubuntu/boot-jar.log 2>&1 &
end script
Where /home/ubuntu/start is the following shell script:
#!/bin/sh
sudo java -jar /home/ubuntu/myjar.jar
exit 0
When this runs, either manually via:
sudo start testjob
or automatically by rebooting the system, I get the following output to the log file:
Exception in thread "main" java.lang.ExceptionInInitializerError
... <bunch of meaningless classloading stuff>
Caused by: java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
at sun.awt.HeadlessToolkit.getMenuShortcutKeyMask(HeadlessToolkit.java:236)
at seesaw.keystroke$preprocess_descriptor.invoke(keystroke.clj:25)
at seesaw.keystroke$keystroke.invoke(keystroke.clj:50)
at seesaw.keystroke$keystroke.invoke(keystroke.clj:49)
at seesaw.keymap$map_key.doInvoke(keymap.clj:107)
at clojure.lang.RestFn.invoke(RestFn.java:445)
at seesaw.widgets.log_window$log_window.doInvoke(log_window.clj:88)
at clojure.lang.RestFn.invoke(RestFn.java:457)
at physicloud.utils__init.load(Unknown Source)
at physicloud.utils__init.<clinit>(Unknown Source)
... 52 more
I don't actually make any UI calls, but the run fails on classloading of the library. I know that a simple fix would be to remove the Java Swing code from the library, but the library is generalized to allow ui output to capable machines, and therefore I am looking for a workaround. It doesn't make sense to me why the jar will run via java -jar but not in the script.
I tried different cases for the upstart script's "start on" condition, all of which produced the same result.
I tried setting the $DISPLAY environment variable in the upstart script before executing the jar, but to no avail.
I also tried scheduling the start script using crontab #reboot, but the error was the same.
Anyone have suggestions?
You can pass -Djava.awt.headless=true to java to allow running non-GUI applications that happen to depend on some UI libraries.
For example,
java -Djava.awt.headless=true -jar something.jar

Remote debugging in eclipse issue

echo "LSE Parser v1.0"
javac -d ../build -classpath ../build:${MY_CLASSPATH} *.java
cd ../build
jar -cf ../lib/lse.jar .
cd ../src
The above is my build script for a java application. there is a serperate run file which has all the CLASSPATH defined and runs the application.
I am trying to remote debug the application on eclipse but facing issues. The code is sitting on unix server host. i've closely followed instructions about debug configurations... for eclipse.
This is what I'm doing:
I locate my build (above) in putty terminal and execute with . ./build
I type java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9999
On eclipse debug config... I enter a random name, project is lse, gave correct host and gave port number as 9999. checked termination of remote VM. When I click debug, it does not stop on the breakpoint.
NOTE: in the project "lse" are 3 .java source that the application uses. the "lse" project has only src content, however in the Unix host it has all the lib, scripts, config folder.
Please can someone tell me what I'm doing wrong. after step 2, I managed to get
Listening for transport dt_socket at address: 9999
then after step 3, I get some messages on putty
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
-client to select the "client" VM
-server to select the "server" VM
-hotspot is a synonym for the "client" VM [deprecated]
The default VM is server,
because you are running on a server-class machine.
....
and see on eclipse a terminated status on debug mode. java hotspot server [host bla bla]
I imagine you solved this by now...but looks like you forgot to provide the name of the class for java to run. If your main method was in a source file like foo/Bar.java, then you would need to add foo.Bar to your java line. Something like:
java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9999 foo.Bar
You might also consider adding -g to your javac to generate debug info.
You could avoid all (or most) of this pain if your source was in the IDE that you're trying to debug with...

Java headless exception when I start java using a linux daemon

I have a linux daemon that starts a java instance for each user that is logged on the system.
I use the followig command to start the java application for each user:
cd /home/user/java_path/ && sudo -u user /opt/jdk1.7.0_17/java -jar software.jar &
The java application monitors a folder and opens a frame for authentication when a new document is create. Each java application reads only the users files (by a name pattern). When tries to open a frame, but I get this error (on a log):
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
java.awt.HeadlessException: ...
When I execute the java application in a regular command line (terminal), this works well.
I saw also the export variables, using export -p command and the display variable is ok for all users logged.
Some java classes depend on having i.e. display device and if you don't have any(running without X-server), they fail with the error you're getting.
Run java with following parameter:
-Djava.awt.headless=true
More info here:
http://www.oracle.com/technetwork/articles/javase/headless-136834.html
The code worked. To resolve that, I had need to export java variables and then starts the java application, in a sequence, like that: cd path && export DISPLAY=:0.0 && export USERNAME= user && sudo -u user javapath/java -jar app.jar >/dev/null 2>&1
Use System.setProperty("java.awt.headless", "true"); or -Djava.awt.headless=true JVM arg

Why does a jar file work differently with and without console?

I obtain different results using the same program when I run it thru:
1)..\Desktop\app.jar
2)..\Desktop\java -jar app.jar
second case gives the same result that I saw in eclipse but it uses console.
Question is: How do I force my program to work properly (to give the same result as in eclipse) by direct executing app.jar (without console)?
Use javaw instead of java in command line:
..\Desktop\javaw -jar app.jar
More information: http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/java.html

Categories

Resources