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

时间:2022-11-13 22:53:10

I use ant for 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“Run”和“Debug” menus执行指定Java类的main()方法......但它们是从包含已编译类文件的目录执行的,而不是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对我的其他问题的回答。)

4 个解决方案

#1


30  

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

是的,您可以创建自定义的“运行配置”:即“Java应用程序”,其中包含:

  • Classpath tab emptied from its default content (the .class directory) and with the jar added
  • Classpath选项卡从其默认内容(.class目录)清空并添加了jar

  • Source tab with its default content (should reference the src directory of the project)
  • Source选项卡及其默认内容(应引用项目的src目录)

One such configuration can be run or debugged.

可以运行或调试一个这样的配置。

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

(Example of a custom configuration with jars as user entries)

(将jar作为用户条目的自定义配置示例)

#2


60  

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连接到该端口

#3


4  

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

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

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

Eclipse configuration if you start the target vm as server

如果将目标vm作为服务器启动,则为Eclipse配置

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

The article gives also a gently introduction into the topic.

文章还简要介绍了该主题。

#4


0  

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类路径中。问题被淘汰了!

#1


30  

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

是的,您可以创建自定义的“运行配置”:即“Java应用程序”,其中包含:

  • Classpath tab emptied from its default content (the .class directory) and with the jar added
  • Classpath选项卡从其默认内容(.class目录)清空并添加了jar

  • Source tab with its default content (should reference the src directory of the project)
  • Source选项卡及其默认内容(应引用项目的src目录)

One such configuration can be run or debugged.

可以运行或调试一个这样的配置。

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

(Example of a custom configuration with jars as user entries)

(将jar作为用户条目的自定义配置示例)

#2


60  

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连接到该端口

#3


4  

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

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

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

Eclipse configuration if you start the target vm as server

如果将目标vm作为服务器启动,则为Eclipse配置

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

The article gives also a gently introduction into the topic.

文章还简要介绍了该主题。

#4


0  

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类路径中。问题被淘汰了!