简体   繁体   English

调试Java项目(其中.jar文件包含main方法)

[英]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. 对于游戏树上的课堂练习,我必须编写与.jar文件配合使用的代码。 I haven't used Java in this way before. 我以前没有以这种方式使用过Java。 My Eclipse project tree looks like this: 我的Eclipse项目树如下所示:

项目树

To run the code, I was told to do this on the command line: 为了运行代码,有人告诉我在命令行上执行此操作:

java -jar VierOpEenRij.jar Mens spel.speler.Random 5 5 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. 我尝试修改Eclipse的调试配置,也尝试执行jdb -jar VierOpEenRij.jar Mens spel.speler.Random 5 5但是我不知道启动调试器的方法。

How do I debug my code when a .jar file contains the main method? .jar文件包含main方法时,如何调试代码?

I'm not sure about debugging with jdb , but if you're debugging in Eclipse, it won't matter what the final packaging(eg jar) is. 我不确定使用jdb调试,但是如果您在Eclipse中进行调试,则最终包装(例如jar)的大小无关紧要。 If you set up the debugging for the Random (class with the main method?) class then you will be debugging your program just fine. 如果您为Random(带有main方法的类?)类设置调试,那么您将可以对程序进行调试。

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? 我可能还没有完全离开,因为我还没有真正遇到过这种情况,但是不会右键单击jar-> debug as ...-> Java应用程序能解决问题吗?

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) 需要一个“半”可运行的jar(似乎是您的情况:主类在其中,但是缺少依赖项,清单应该指示一个主类)
  • add the main to the buidl path (in package explorer view, right click on jar -> build-path -> add to build path). 将main添加到buidl路径(在资源管理器视图中,右键单击jar-> 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. 在包资源管理器视图的“构建路径”节点中,展开jar,直到您向下钻取到main方法,右键单击并作为Java应用程序运行或调试。

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. 出于某种原因,我希望能够右键单击jar并直接将其调试为java应用程序……主要是在清单中指出,但似乎找不到它。 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. EDIT2:实际上,现在我可以直接右键单击jar(甚至项目),并在弹出对话框时选择正确的main,这一切都很好。 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. 在下面的图片中,MainInJar.main()调用ClassOutOfJar.somethingOutOfJar(),将某些内容打印到屏幕上。

主要在罐子里

You need to specify classpath in jdb. 您需要在jdb中指定classpath。

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

In your case the jar file will be: VierOpEenRij.jar 在您的情况下,jar文件将为:VierOpEenRij.jar

Main class name you could find in jar archive in META-INF/MANIFEST.MF. 您可以在META-INF / MANIFEST.MF的jar存档中找到主类名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM