Pieter
Pieter

Reputation: 32755

Debugging Java projects where a .jar file contains the main method

For a class exercise on game trees, I have to write code that works with a .jar file. I haven't used Java in this way before. My Eclipse project tree looks like this:

Project tree

To run the code, I was told to do this on the command line:

java -jar VierOpEenRij.jar Mens spel.speler.Random 5 5

How do I debug this code? I have tried tinkering with Eclipse's debug configurations and I also tried executing jdb -jar VierOpEenRij.jar Mens spel.speler.Random 5 5 but I can't figure out a way to start the debugger.

How do I debug my code when a .jar file contains the main method?

Upvotes: 2

Views: 3133

Answers (3)

Wojciech Kałuski
Wojciech Kałuski

Reputation: 312

You need to specify classpath in jdb.

jdb -classpath [jar-file] [main-class-full-name] [args]

In your case the jar file will be: VierOpEenRij.jar

Main class name you could find in jar archive in META-INF/MANIFEST.MF.

Upvotes: 0

haylem
haylem

Reputation: 22663

I might be completely off as I haven't really been in this situation, but wouldn't right-click on the jar -> debug as ... -> Java application do the trick?

EDIT:

Managed to make it work this way:

  • need a "semi" runnable jar (seems to be your case: main class in there but missing dependencies, and the manifest should indicate a main class)
  • add the main to the buidl path (in package explorer view, right click on jar -> build-path -> add to build path).
  • in the package explorer view's build path node, expand the jar until your drill down to the main method, right click and run or debug as java application.

For some reason I would expect to be able to right-click the jar and do directly dbug as java app though... the main is indicated in the manifest, but it doesn't seem to find it. Don't know why. But in the meantime, that works.

EDIT2: Actually, now I can directly right-click the jar (or even the project) and select the right main when a dialog pops up, and it's all good. Maybe I missed something earlier. Any way, you're good to go.)

In the picture below, MainInJar.main() calls ClassOutOfJar.somethingOutOfJar(), which prints something to the screen.

Main in Jar

Upvotes: 1

Poindexter
Poindexter

Reputation: 2425

I'm not sure about debugging with jdb, but if you're debugging in Eclipse, it won't matter what the final packaging(e.g. jar) is. If you set up the debugging for the Random (class with the main method?) class then you will be debugging your program just fine.

Upvotes: 2

Related Questions