[java] how can I debug a jar at runtime?

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.

This question is related to java eclipse debugging jar runtime

The answer is


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

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.


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.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to eclipse

How do I get the command-line for an Eclipse run configuration? My eclipse won't open, i download the bundle pack it keeps saying error log strange error in my Animation Drawable How to uninstall Eclipse? How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Class has been compiled by a more recent version of the Java Environment Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory How to downgrade Java from 9 to 8 on a MACOS. Eclipse is not running with Java 9 "The POM for ... is missing, no dependency information available" even though it exists in Maven Repository The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat

Examples related to debugging

How do I enable logging for Spring Security? How to run or debug php on Visual Studio Code (VSCode) How do you debug React Native? How do I debug "Error: spawn ENOENT" on node.js? How can I inspect the file system of a failed `docker build`? Swift: print() vs println() vs NSLog() JavaScript console.log causes error: "Synchronous XMLHttpRequest on the main thread is deprecated..." How to debug Spring Boot application with Eclipse? Unfortunately MyApp has stopped. How can I solve this? 500 internal server error, how to debug

Examples related to jar

Running JAR file on Windows 10 Add jars to a Spark Job - spark-submit Deploying Maven project throws java.util.zip.ZipException: invalid LOC header (bad signature) java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory Maven Error: Could not find or load main class Where can I download mysql jdbc jar from? Access restriction: The type 'Application' is not API (restriction on required library rt.jar) Create aar file in Android Studio Checking Maven Version Creating runnable JAR with Gradle

Examples related to runtime

how can I debug a jar at runtime? How to check whether java is installed on the computer Excel VBA Code: Compile Error in x64 Version ('PtrSafe' attribute required) Excel VBA Run-time error '424': Object Required when trying to copy TextBox Execute external program An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Unknown Module Error when checking Java version: could not find java.dll Change the location of an object programmatically java.lang.NoClassDefFoundError: Could not initialize class XXX How permission can be checked at runtime without throwing SecurityException?