eclipse:如何将 Java 程序调试为 .jar 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1732259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:00:22  来源:igfitidea点击:

eclipse: how to debug a Java program as a .jar file?

javaeclipsejar

提问by Jason S

I use antfor creating .jar files in Eclipse. Works great.

ant用于在 Eclipse 中创建 .jar 文件。效果很好。

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?

我有一个我正在处理的 .jar 文件,它希望代码位于 .jar 文件中(它在与 .jar 文件相同的目录中查找 .properties 文件)——标准的 Eclipse“运行”和“调试” menus 执行main()指定 Java 类的方法...但它们是从包含已编译类文件的目录中执行的,而不是 jar 文件。有没有办法更改此行为,以便 Eclipse 运行来自相应 .jar 文件的代码?

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

(我现在的解决方法是在外部运行 .jar 文件,根据Dave Ray 对我的其他问题之一的回答,它暂停等待调试器。)

采纳答案by VonC

Yes, you can create a custom "Run Configuration":
Ie, a "Java Application" one, with:

是的,您可以创建一个自定义的“ Run Configuration”:
即一个“ Java Application”,具有:

  • Classpathtab emptied from its default content (the .classdirectory) and with the jar added
  • Sourcetab with its default content (should reference the srcdirectory of the project)
  • Classpath选项卡从其默认内容(.class目录)中清空并添加了 jar
  • Source选项卡及其默认内容(应引用src项目的目录)

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

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)

使用 jars 作为用户条目的自定义配置示例

回答by broschb

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!

我会尝试使代码更健壮,使属性文件位置可配置,或者只是让它从类路径加载它。然后你可以直接将属性文件添加到 eclipse 类路径中。问题解决!

回答by IonSpin

You could use remote debugging by running your jar like this

您可以通过像这样运行 jar 来使用远程调试

java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 -jar yourJar.jar

And then connecting from your IDE to that port

然后从您的 IDE 连接到该端口

回答by Alessandro Giusa

I just found the following link, which describes the whole procedure in order to debug a Java jar remotely.

我刚刚找到了以下链接,它描述了远程调试 Java jar 的整个过程。

Debug Java applications remotely with Eclipse

使用 Eclipse 远程调试 Java 应用程序

The main parts are:

主要部分是:

Target VM acts as debug server

目标 VM 充当调试服务器

java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address="8000" -jar test.jar

java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address="8000" -jar test.jar

Target VM acts as debug client

目标 VM 充当调试客户端

java -Xdebug -Xrunjdwp:transport=dt_socket,address=127.0.0.1:8000,suspend=y -jar test.jar

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。

Eclipse configuration if you start the target vm as client

如果您将目标虚拟机作为客户端启动,则 Eclipse 配置

enter image description here

在此处输入图片说明

Eclipse configuration if you start the target vm as server

如果您将目标虚拟机作为服务器启动,则 Eclipse 配置

enter image description here

在此处输入图片说明

The article gives also a gently introduction into the topic.

文章还对该主题进行了温和的介绍。