Java 如何在运行时调试 jar?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19842322/
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 20:37:37  来源:igfitidea点击:

how can I debug a jar at runtime?

javaeclipsedebuggingjarruntime

提问by eewing

I'm into a quite strange position (from my java-newbie point of view):

我处于一个非常奇怪的位置(从我的 java 新手的角度来看):

  1. using Eclipse I wrote a "java program" (some .java files with classes into) which essentially (batch) reads a text *.csv file, "evaluates" its contents, and writes out results into an *_out.csv text file. To locate the input file it uses a "file chooser" (got sample from here: http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html)

  2. I debugged all the code and, using the debugger, it works.

  3. I ran the code (the main class, which calls all the other in sequence) and it works, while in Eclipse.

  4. I exported all the project's contents into a "runnable jar" file.

  1. 使用 Eclipse 我编写了一个“java 程序”(一些带有类的 .java 文件),它基本上(批处理)读取文本 *.csv 文件,“评估”其内容,并将结果写出到 *_out.csv 文本文件中。要定位输入文件,它使用“文件选择器”(从这里获取示例:http: //docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html

  2. 我调试了所有代码,并且使用调试器,它可以工作

  3. 我跑的代码(主类,它调用所有其他的序列)和它的作品而在Eclipse中

  4. 我将项目的所有内容导出到一个“可运行的 jar”文件中。

Notice that, file chooser apart, this is mainly a "batch" which reads and writes: nearly no User Interface at all. While into Eclipse I shown some internal results using something like "if(debug) System.out.print("something to print");" providing to set "debug" TRUE while debugging and FALSE while in production environment.

请注意,除了文件选择器之外,这主要是一个读取和写入的“批处理”:几乎没有用户界面。在 Eclipse 中,我使用诸如“ if(debug) System.out.print("something to print"); 之类的东西显示了一些内部结果提供在调试时将“调试”设置为TRUE,而在生产环境中设置为FALSE。

ALL of the above worked!

以上所有工作!

Now, starting the runnable jar (double-click on the jar file, in Win/XP), I can seethe file chooser and I can use it but, after choosing the input file... nothing more: (having no user interface) I don't know if the file was read, I don't see any generated output file, and I've even no "console" to list any intermediate debug message, to see if the jar is working, even if I re-export it with the debug variable set to TRUE.

现在,启动可运行的 jar(双击 jar 文件,在 Win/XP 中),我可以看到文件选择器,我可以使用它,但是,在选择输入文件后...仅此而已:(没有用户界面) 我不知道文件是否被读取,我没有看到任何生成的输出文件,我什至没有“控制台”来列出任何中间调试消息,看看 jar 是否正在工作,即使我重新- 在调试变量设置为 TRUE 的情况下导出它。

Is there a way to "runtime debug" the running jar (like VB's MsgBox, or something other)? some kind of "log file" I can "enable" or look into? (obviously, as my jar is not writing the result file, I just can't try writing a *.log too) I have also to say I just can'tinstall other than Eclipse on my machine (and just been lucky it ran), so no usual developer's tools, utilities and other useful things.

有没有办法“运行时调试”正在运行的 jar(比如 VB 的 MsgBox 或其他东西)?我可以“启用”或查看某种“日志文件”?(很明显,因为我的 jar 没有写结果文件,我也不能尝试写一个 *.log)我还得说我不能在我的机器上安装 Eclipse 以外的东西(而且很幸运它运行了),因此没有通常的开发人员工具、实用程序和其他有用的东西。

采纳答案by arcy

Even though it is a runnable jar, you can still run it from a console -- open a terminal window, navigate to the directory containing the jar, and enter "java -jar yourJar.jar". It will run in that terminal window, and sysout and syserr output will appear there, including stack traces from uncaught exceptions. Be sure to have your debug set to true when you compile. And good luck.

即使它是一个可运行的 jar,您仍然可以从控制台运行它——打开一个终端窗口,导航到包含 jar 的目录,然后输入“java -jar yourJar.jar”。它将在该终端窗口中运行,并且 sysout 和 syserr 输出将出现在那里,包括来自未捕获异常的堆栈跟踪。请确保在编译时将调试设置为 true。还有祝你好运。



Just thought of something else -- if you're on Win7, it often has permission problems with user applications writing files to specific directories. Make sure the directory to which you are writing your output file is one for which you have permissions.

只是想到别的东西——如果你在 Win7 上,用户应用程序将文件写入特定目录通常会出现权限问题。确保您将输出文件写入的目录是您拥有权限的目录。

In a future project, if it's big enough, you can use one of the standard logging facilities for 'debug' output; then it will be easy(ier) to redirect it to a file instead of depending on having a console. But for a smaller job like this, this should be fine.

在未来的项目中,如果它足够大,您可以使用标准日志记录工具之一进行“调试”输出;那么将它重定向到一个文件而不是依赖于一个控制台会很容易(ier)。但是对于像这样的小型工作,这应该没问题。

回答by RamonBoza

http://www.eclipsezone.com/eclipse/forums/t53459.html

http://www.eclipsezone.com/eclipse/forums/t53459.html

Basically run it with:

基本上运行它:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=1044

The application, at launch, will wait until you connect from another source.

该应用程序在启动时将等待您从其他来源连接。

回答by Abdull

You can activate JVM's debugging capability when starting up the javacommand with a special option:

您可以在java使用特殊选项启动命令时激活 JVM 的调试功能:

java -agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=y -jar path/to/some/war/or/jar.jar

Starting up jar.jarlike that on the command line will:

jar.jar像这样在命令行上启动将:

  • put thisJVM instance in the role of a server (server=y) listening on port 8000 (address=8000)
  • write Listening for transport dt_socket at address: 8000to stdoutand
  • then pause the application (suspend=y) until some debugger connects. The debugger acts as the clientin this scenario.
  • 这个JVM实例的服务器(角色server=y端口8000)听(address=8000
  • 写信Listening for transport dt_socket at address: 8000stdout
  • 然后暂停应用程序 ( suspend=y) 直到某个调试器连接。在这种情况下,调试器充当客户端

Common options for selecting a debugger are:

选择调试器的常见选项有:

  • Eclipse Debugger: Under Run-> Debug Configurations...-> select Remote Java Application-> click the New launch configurationbutton. Provide an arbitrary Namefor this debug configuration, Connection Type: Standard (Socket Attach)and as Connection Propertiesthe entries Host: localhost, Port: 8000. Applythe Changes and click Debug. At the moment the Eclipse Debugger has successfully connected to the JVM, jar.jarshould begin executing.
  • jdb command-line tool: Start it up with jdb -connect com.sun.jdi.SocketAttach:port=8000
  • Eclipse Debugger:在Run-> Debug Configurations...-> 选择Remote Java Application-> 单击New launch configuration按钮。为此调试配置提供任意名称连接类型:标准(套接字连接)和条目主机:本地主机端口:8000作为连接属性应用更改并单击调试。此时 Eclipse Debugger 已成功连接到 JVM,应该开始执行。jar.jar
  • jdb 命令行工具:启动它jdb -connect com.sun.jdi.SocketAttach:port=8000

回答by BullyWiiPlaza

With IntelliJ IDEAyou can create a Jar Applicationruntime configuration, select the JAR, the sources, the JREto run the Jarwith and start debugging. Hereis the documentation.

随着IntelliJ IDEA你可以创建一个Jar Application运行时配置,选择JAR,该人士透露,JRE运行Jar与和启动调试。是文档。