38

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

5 Answers 5

70

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

Sign up to request clarification or add additional context in comments.

2 Comments

This one worked for me; the Java application launch required a main class.
In Eclipse one goes to "Debug Configurations..." and creates a new "Remote Java Application".
32

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)

5 Comments

That is not applicable to IntelliJ Idea, right? (it requires a module to be specified)
@den: not directly applicable (I don't know enough IntelliJ to immediately think of a similar procedure)
_@VonC: in idea you can not run application which is not one of your modules (projects in eclipse "language", I guess). It appears a working solution is setting up a remote application run.
@den: correct, a remote app should work (with any IDE in fact)
This sounds like a great solution, but i think you are required to supply a Main class; i think IonSpin's answer below is equivalent to this, but without the need to specify a main class when doing remote debugging.
9

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

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.

Comments

0

If none of this solved, mine solved by simply clicking the debug thread on debug console.

Comments

-1

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!

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.