Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

eclipse: how to debug a Java program as a .jar file?

Tags:

java

eclipse

jar

I use ant for creating .jar files in Eclipse. Works great.

I have a .jar file I am working on that expects the code to be in a .jar file (it looks for .properties files in the same directory as the .jar file) -- the standard Eclipse "Run" and "Debug" menus execute the main() method of a specified Java class... but they do it from the directory containing the compiled class files, not a jar file. Is there a way to change this behavior so Eclipse runs code from the appropriate .jar file instead?

(My workaround right now is to run the .jar file externally, with it suspended waiting for a debugger, per Dave Ray's answer to one of my other questions.)

like image 953
Jason S Avatar asked Nov 13 '09 22:11

Jason S


People also ask

Can we debug jar in Eclipse?

First open the jar file using JD(Java Decompiler), then click on File -> Save JAR Sources and it will become a . zip file containing the java source files. Then you can add this source zip file in the debug configuration using add source. It will work then for sure.


2 Answers

You could use remote debugging by running your jar like this

java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 -jar yourJar.jar 

And then connecting from your IDE to that port

like image 63
IonSpin Avatar answered Sep 20 '22 14:09

IonSpin


Yes, you can create a custom "Run Configuration":
Ie, a "Java Application" one, with:

  • Classpath tab emptied from its default content (the .class directory) and with the jar added
  • Source tab with its default content (should reference the src directory of the project)

One such configuration can be run or debugged.

http://www.kermeta.org/docs/html.chunked/KerMeta-UI-UserGuide/KerMeta-UI-UserGuide_figures/KerMeta_RunCommandLine_classpath.png

(Example of a custom configuration with jars as user entries)

like image 20
VonC Avatar answered Sep 19 '22 14:09

VonC