[英]how can I debug a jar at runtime?
I'm into a quite strange position (from my java-newbie point of view): 我从一个非常奇怪的位置(从我的java-newbie观点来看):
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. 使用Eclipse我写了一个“java程序”(一些带有类的.java文件),它基本上(批处理)读取文本* .csv文件,“评估”其内容,并将结果写入* _out.csv文本文件。 To locate the input file it uses a "file chooser" (got sample from here: http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html )
要找到输入文件,它使用“文件选择器”(从此处获取示例: http : //docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html )
I debugged all the code and, using the debugger, it works . 我调试了所有代码,并使用调试器, 它的工作原理 。
I ran the code (the main class, which calls all the other in sequence) and it works , while in Eclipse . 我运行了代码(主类,它按顺序调用所有其他代码), 它 在Eclipse中运行 。
I exported all the project's contents into a "runnable jar" file. 我将所有项目的内容导出到“runnable 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(”要打印的东西“)的内容显示了一些内部结果; ”在生产环境中提供设置“ debug ”为TRUE而调试时为FALSE。
ALL of the above worked! 所有上述工作!
Now, starting the runnable jar (double-click on the jar file, in Win/XP), I can see the 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. 现在,启动runnable jar(双击jar文件,在Win / XP中), 我可以看到文件选择器,我可以使用它但是,在选择输入文件之后...... 仅此而已 :(没有用户界面) )我不知道文件是否被读取,我没有看到任何生成的输出文件,我甚至没有“控制台”列出任何中间调试消息,看看jar是否正常工作,即使我重新-export,调试变量设置为TRUE。
Is there a way to "runtime debug" the running jar (like VB's MsgBox, or something other)? 有没有办法“运行时调试”正在运行的jar(如VB的MsgBox,或其他东西)? 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't install other than Eclipse on my machine (and just been lucky it ran), so no usual developer's tools, utilities and other useful things.
(很明显,因为我的jar没有写结果文件,所以我也不能尝试编写* .log)我还要说我不能在我的机器上安装除Eclipse之外的东西(幸运的是它跑了),所以没有通常的开发人员的工具,实用程序和其他有用的东西。
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. 启动时,应用程序将一直等到您从其他来源连接。
You can activate JVM's debugging capability when starting up the java
command 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.jar
like that on the command line will: 在命令行上启动
jar.jar
会:
server=y
) listening on port 8000 ( address=8000
) server=y
)的角色中侦听端口8000( address=8000
) Listening for transport dt_socket at address: 8000
to stdout
and Listening for transport dt_socket at address: 8000
到stdout
和 suspend=y
) until some debugger connects. suspend=y
)直到某个调试器连接。 The debugger acts as the client in this scenario. Common options for selecting a debugger are: 选择调试器的常用选项有:
jar.jar
should begin executing. jar.jar
应该开始执行。 jdb -connect com.sun.jdi.SocketAttach:port=8000
jdb -connect com.sun.jdi.SocketAttach:port=8000
启动它 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". 即使它是一个可运行的jar,你仍然可以从控制台运行它 - 打开一个终端窗口,导航到包含jar的目录,然后输入“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.
它将在该终端窗口中运行,并且sysout和syserr输出将出现在那里,包括来自未捕获异常的堆栈跟踪。 Be sure to have your debug set to true when you compile.
编译时一定要将调试设置为true。 And good luck.
还有祝你好运。
Just thought of something else -- if you're on Win7, it often has permission problems with user applications writing files to specific directories. 只是想到了别的东西 - 如果你在Win7上,它通常会有用户应用程序将文件写入特定目录的权限问题。 Make sure the directory to which you are writing your output file is one for which you have permissions.
确保您编写输出文件的目录是您具有权限的目录。
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.
然后将它重定向到文件而不是依赖于控制台将很容易(ier)。 But for a smaller job like this, this should be fine.
但对于像这样的小工作,这应该没问题。
With IntelliJ IDEA
you can create a Jar Application
runtime configuration, select the JAR
, the sources, the JRE
to run the Jar
with and start debugging. 使用
IntelliJ IDEA
您可以创建Jar Application
运行时配置,选择JAR
,源代码,运行Jar
的JRE
并开始调试。 Here is the documentation. 这是文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.