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

0 votes
asked Nov 13, 2009 by jason-s

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.)

4 Answers

0 votes
answered Nov 13, 2009 by vonc

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.

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

0 votes
answered Nov 14, 2009 by broschb

I would try to make the code more robust, make the properties file location configurable, or just make it load it from the classpath. Then you can just directly add the properties file to the eclipse classpath. Problem Sovled!

0 votes
answered Nov 9, 2011 by ionspin

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

0 votes
answered Sep 15, 2017 by alessandro-giusa

I just found the following link, which describes the whole procedure in order to debug a Java jar remotely.

Debug Java applications remotely with Eclipse

The main parts are:

Target VM acts as debug server

java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address="8000" -jar test.jar

Target VM acts as debug client

java -Xdebug -Xrunjdwp:transport=dt_socket,address=,suspend=y -jar test.jar

Based on how you run the target vm, client or server, you have to configure Eclipse differently.

Eclipse configuration if you start the target vm as client

enter image description here

Eclipse configuration if you start the target vm as server

enter image description here

The article gives also a gently introduction into the topic.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter