开发者

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

开发者 https://www.devze.com 2022-12-11 23:15 出处:网络
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开发者_StackOverflow .jar file (it looks for .properties files in the sam

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开发者_StackOverflow .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.)


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


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.

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

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


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=127.0.0.1:8000,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

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

Eclipse configuration if you start the target vm as server

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

The article gives also a gently introduction into the topic.


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

精彩评论

暂无评论...
验证码 换一张
取 消