54

I'm into a quite strange position (from my java-newbie point of view):

  1. using Eclipse I wrote a "java program" (some .java files with classes into) which essentially (batch) reads a text *.csv file, "evaluates" its contents, and writes out results into an *_out.csv text file. To locate the input file it uses a "file chooser" (got sample from here: http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html)

  2. I debugged all the code and, using the debugger, it works.

  3. I ran the code (the main class, which calls all the other in sequence) and it works, while in Eclipse.

  4. I exported all the project's contents into a "runnable jar" file.

Notice that, file chooser apart, this is mainly a "batch" which reads and writes: nearly no User Interface at all. While into Eclipse I shown some internal results using something like "if(debug) System.out.print("something to print");" providing to set "debug" TRUE while debugging and FALSE while in production environment.

ALL of the above worked!

Now, starting the runnable jar (double-click on the jar file, in Win/XP), I can see the file chooser and I can use it but, after choosing the input file... nothing more: (having no user interface) I don't know if the file was read, I don't see any generated output file, and I've even no "console" to list any intermediate debug message, to see if the jar is working, even if I re-export it with the debug variable set to TRUE.

Is there a way to "runtime debug" the running jar (like VB's MsgBox, or something other)? some kind of "log file" I can "enable" or look into? (obviously, as my jar is not writing the result file, I just can't try writing a *.log too) I have also to say I just can't install other than Eclipse on my machine (and just been lucky it ran), so no usual developer's tools, utilities and other useful things.

Andrea
  • 11,801
  • 17
  • 65
  • 72
eewing
  • 545
  • 1
  • 4
  • 5
  • Have a look at Lo4j, for a MsgBox style message use JOptionPane. The reason your program is not running correctly outside eclipse might be that you have no rights (admin?) to read/write at this specific location or your output is in a different folder? – HectorLector Nov 07 '13 at 17:50
  • That should be Log4j, not Lo4j. https://logging.apache.org/log4j/2.0/ – JESii Feb 20 '18 at 14:02

4 Answers4

123

http://www.eclipsezone.com/eclipse/forums/t53459.html

Basically run it with:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=1044

The application, at launch, will wait until you connect from another source.

so the CLI command will be:

java -jar yourJarFileName.jar -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=1044
sifr_dot_in
  • 3,153
  • 2
  • 33
  • 42
RamonBoza
  • 8,898
  • 6
  • 36
  • 48
  • 9
    Do people have any idea how valuable this comment is? – George Jan 19 '15 at 15:15
  • 57
    for dumb people like myself, do a `java -jar -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=1044`. Then go to eclipse debug configurations and create a remote java application configuration which uses 1044 as the port. – trailblazer Oct 07 '15 at 19:02
  • 1
    I added this in my command to launch jar but I still can't connect with jdb to the debug JVM. When I type after jdb -attach 8787 (my port used) it fails with "Connection refused". I launched the app with the dedicated user and this command too (he have the full rights on app). Any ideas ? – Alex Jul 07 '16 at 10:00
  • 7
    @Alex See [my answer](http://stackoverflow.com/a/38357751/923560), which uses another (more modern?) command option (also notice that order of command options [first `-agentlib:...`, then `-jar ...` may be significant). – Abdull Jul 13 '16 at 17:06
  • 8
    As @Abdull stated, the order mattered to me, so `java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=1044 -jar ` – Bruno Calça Sep 14 '18 at 13:47
  • jdb -attach 1044 java.io.IOException: shmemBase_attach failed: ?????????¨ at jdk.jdi/com.sun.tools.jdi.SharedMemoryTransportService.attach0(Native Method) at jdk.jdi/com.sun.tools.jdi.SharedMemoryTransportService.attach(SharedMemoryTransportService.java:108) at jdk.jdi/com.sun.tools.jdi.GenericAttachingConnector.attach(GenericAttachingConnector.java:119) at jdk.jdi/com.sun.tools.jdi.SharedMemoryAttachingConnector.attach(SharedMemoryAttachingConnector.java:63) 。。。。。 at jdk.jdi/com.sun.tools.example.debug.tty.TTY.main(TTY.java:1095) 无法附加到目标 VM。 – CS QGB Jun 09 '22 at 14:13
  • It turns out the order of the command line options matters. you must do java [DEBUG OPTIONS] [-jar JARFILE]. backwards of this will not open the socket. – Matt Brunell Sep 22 '22 at 16:09
70

You can activate JVM's debugging capability when starting up the java command with a special option:

java -agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=y -jar path/to/some/war/or/jar.jar

Starting up jar.jar like that on the command line will:

  • put this JVM instance in the role of a server (server=y) listening on port 8000 (address=8000)
  • write Listening for transport dt_socket at address: 8000 to stdout and
  • then pause the application (suspend=y) until some debugger connects. The debugger acts as the client in this scenario.

Common options for selecting a debugger are:

  • Eclipse Debugger: Under Run -> Debug Configurations... -> select Remote Java Application -> click the New launch configuration button. Provide an arbitrary Name for this debug configuration, Connection Type: Standard (Socket Attach) and as Connection Properties the entries Host: localhost, Port: 8000. Apply the Changes and click Debug. At the moment the Eclipse Debugger has successfully connected to the JVM, jar.jar should begin executing.
  • jdb command-line tool: Start it up with jdb -connect com.sun.jdi.SocketAttach:port=8000
Abdull
  • 26,371
  • 26
  • 130
  • 172
  • 1
    Just adding for jdk 1.4x is -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044 and for jdk 1.3 or earlier is: -Xnoagent -Djava.compiler=NONE -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044 – cabaji99 Sep 26 '16 at 20:45
  • I think this should be selected as correct answer as the exaplanation is precise to the title - "how can I debug the jar file". The other correct answer definitely would have solved the problem but that explanation is different from the title subject line and hence the current marked correct answer may not help to people coming on this thread for looking on the solution of debuginng the executable jar file. – Maulik Kayastha Mar 03 '20 at 16:34
17

Even though it is a runnable jar, you can still run it from a console -- open a terminal window, navigate to the directory containing the jar, and enter "java -jar yourJar.jar". It will run in that terminal window, and sysout and syserr output will appear there, including stack traces from uncaught exceptions. Be sure to have your debug set to true when you compile. And good luck.


Just thought of something else -- if you're on Win7, it often has permission problems with user applications writing files to specific directories. Make sure the directory to which you are writing your output file is one for which you have permissions.

In a future project, if it's big enough, you can use one of the standard logging facilities for 'debug' output; then it will be easy(ier) to redirect it to a file instead of depending on having a console. But for a smaller job like this, this should be fine.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • 1
    Thank you very much rcook, your solution completely solved my problem: I didn't figure that, when the JVM is directly called, it "exports" stdout and stderr to the console (as a matter of fact the problem is not having a local console). The program relies on a couple of text files (configuration and other) which should be "external": when in Eclipse the files was available to the filesystem but when in JAR they wasn't. Now I copied the textfiles out of the JAR and the program immediately worked. :-) @RamonBoza: I've really little rights on my machine and no rights on networked ones. :-/ – eewing Nov 08 '13 at 14:09
  • Just FYI: files that you don't need to write to can be put in the jar, and then accessed as "resources" -- look up a tutorial on Class.getResource() and related methods. Am glad this helped. – arcy Nov 08 '13 at 15:00
  • Its helps a lot, if you are on windows 10 and have Control Folder Access enabled from windows defender then it cause the same issue. Just gave an access to your application. – maazakn Sep 27 '20 at 10:37
2

With IntelliJ IDEA you can create a Jar Application runtime configuration, select the JAR, the sources, the JRE to run the Jar with and start debugging. Here is the documentation.

BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185