开发者

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

开发者 https://www.devze.com 2023-01-26 15:22 出处:网络
For a class exercise on game trees, I have to write code that works with a .j开发者_如何学JAVAar file. I haven\'t used Java in this way before. My Eclipse project tree looks like this:

For a class exercise on game trees, I have to write code that works with a .j开发者_如何学JAVAar file. I haven't used Java in this way before. My Eclipse project tree looks like this:

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

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?


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.


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.

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


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.

0

精彩评论

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