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

Community
  • 1
  • 1
Jason S
  • 184,598
  • 164
  • 608
  • 970

4 Answers4

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

IonSpin
  • 1,255
  • 1
  • 14
  • 16
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)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • That is not applicable to IntelliJ Idea, right? (it requires a module to be specified) – Denys S. Jul 11 '11 at 13:42
  • @den: not directly applicable (I don't know enough IntelliJ to immediately think of a similar procedure) – VonC Jul 11 '11 at 13:47
  • 1
    _@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. – Denys S. Jul 11 '11 at 13:54
  • @den: correct, a remote app should work (with any IDE in fact) – VonC Jul 11 '11 at 13:56
  • 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. – acostache Nov 19 '12 at 13:07
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.

Alessandro Giusa
  • 1,660
  • 15
  • 11
-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!

broschb
  • 4,976
  • 4
  • 35
  • 52