简体   繁体   English

我怎样才能在运行时调试jar?

[英]how can I debug a jar at runtime?

I'm into a quite strange position (from my java-newbie point of view): 我从一个非常奇怪的位置(从我的java-newbie观点来看):

  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. 使用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

  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 . 我运行了代码(主类,它按顺序调用所有其他代码), 在Eclipse中运行

  4. 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会:

  • put this JVM instance in the role of a server ( server=y ) listening on port 8000 ( address=8000 ) 将此 JVM实例置于服务器( server=y )的角色中侦听端口8000( address=8000
  • write Listening for transport dt_socket at address: 8000 to stdout and 写入Listening for transport dt_socket at address: 8000stdout
  • then pause the application ( suspend=y ) until some debugger connects. 然后暂停应用程序( suspend=y )直到某个调试器连接。 The debugger acts as the client in this scenario. 在此方案中,调试器充当客户端

Common options for selecting a debugger are: 选择调试器的常用选项有:

  • Eclipse Debugger: Under Run -> Debug Configurations... -> select Remote Java Application -> click the New launch configuration button. Eclipse Debugger:在Run - > Debug Configurations ...下 - 选择Remote Java Application - >单击New launch configuration按钮。 Provide an arbitrary Name for this debug configuration, Connection Type: Standard (Socket Attach) and as Connection Properties the entries Host: localhost , Port: 8000 . 为此调试配置提供任意名称连接类型:标准(套接字连接)连接属性条目主机:localhost端口:8000 Apply the Changes and click Debug . 应用更改并单击“ 调试” At the moment the Eclipse Debugger has successfully connected to the JVM, jar.jar should begin executing. 在Eclipse Debugger成功连接到JVM的那一刻, jar.jar应该开始执行。
  • jdb command-line tool : Start it up with jdb -connect com.sun.jdi.SocketAttach:port=8000 jdb命令行工具 :使用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 ,源代码,运行JarJRE并开始调试。 Here is the documentation. 是文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何调试外部JAR文件 - How can I debug external JAR files 如何使用调试信息构建asm.jar? - How can I build asm.jar with debug information? 如何在运行时知道JAR文件是否已存在于类路径中? - How can I know at runtime if a JAR file is already in the classpath? 如何从编译的 jar 中删除运行时依赖项 - How can I remove runtime dependencies from compiled jar 如何在运行时调试我的Android应用程序的RAM消耗? - How can I debug RAM consumption of my android application in runtime? 如何在运行时使用 Java 调试接口从 Jar 文件加载 class - How to load a class from a Jar file at runtime with the Java Debug Interface 在Linux上远程调试jar时如何使用参数 - How can I use arguments, when I remote debug a jar on Linux 如何设置Scala / Java工作流以通过Jumpserver远程部署jar和进行远程调试? - How can I set up a Scala/Java workflow to remote deploy jar and remote debug THROUGH a jumpserver? 如何获取运行时依赖项以及 Gradle 项目的坐标和 JAR 文件? - How can I get a the runtime dependencies along with the coordinates and the JAR file of a Gradle project? 用于构建可执行jar的通用pom.xml:如何在运行时指定依赖项groupId / artifactId / version? - Generic pom.xml for building an executable jar: How can I specify dependency groupId/artifactId/version at runtime?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM