调试 .jar 文件包含 main 方法的 Java 项目
对于游戏树的课堂练习,我必须编写适用于的代码.jar 文件。我以前没有以这种方式使用过Java。我的 Eclipse 项目树如下所示:
要运行代码,我被告知要在命令行上执行此操作:
java -jar VierOpEenRij.jar 男士 spel.speler.Random 5 5
如何调试此代码?我尝试修改 Eclipse 的调试配置,还尝试执行 jdb -jar VierOpEenRij.jar Mens spel.speler.Random 5 5 但我无法找到启动调试器的方法。
当 .jar 文件包含 main 方法时,如何调试我的代码?
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:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定是否使用
jdb
进行调试,但是如果您在Eclipse中进行调试,那么最终的打包(例如jar)是什么并不重要。如果您为 Random(具有 main 方法的类?)类设置调试,那么您将可以很好地调试您的程序。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.我可能完全关闭了,因为我还没有真正遇到过这种情况,但不会右键单击罐子 ->调试为... -> Java 应用程序能解决这个问题吗?
编辑:
设法使其以这种方式工作:
出于某种原因,我希望能够右键单击 jar 并直接作为 java 应用程序进行调试... main 在清单中指出,但似乎没有找到它。不知道为什么。但与此同时,这确实有效。
EDIT2:实际上,现在我可以直接右键单击 jar (甚至项目)并在弹出对话框时选择正确的 main ,这一切都很好。也许我之前错过了一些东西。不管怎样,你已经可以开始了。)
在下图中,MainInJar.main() 调用 ClassOutOfJar.somethingOutOfJar(),它会在屏幕上打印一些内容。
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:
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.
您需要在 jdb 中指定类路径。
在您的情况下,jar 文件将为: VierOpEenRij.jar
您可以在 META-INF/MANIFEST.MF 的 jar 存档中找到主类名。
You need to specify classpath in jdb.
In your case the jar file will be: VierOpEenRij.jar
Main class name you could find in jar archive in META-INF/MANIFEST.MF.