在 Eclipse 中使用 ANT 脚本时如何调试 Java 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2406825/
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
How to debug Java code when using ANT script in Eclipse
提问by zengr
I have a java class and I need to debug it (put breakpoints and continue using F6). I am using ANT script to init, build, deploy and run the code. I am using:
我有一个 java 类,我需要调试它(放置断点并继续使用 F6)。我正在使用 ANT 脚本来初始化、构建、部署和运行代码。我在用:
<javac srcdir="${src.dir}" destdir="${classes.dir}" debug="true" debuglevel="lines,vars,source">
..........
</javac>
But when I place the breakpoint in a line in my foo.java class and I run the ant script (the run part, Right Click on run-->Debug As-->Ant Build), Eclipse does not stop at that line of code.
但是,当我将断点放在 foo.java 类中的一行中并运行 ant 脚本(运行部分,右键单击 run-->Debug As-->Ant Build)时,Eclipse 不会在该行停止代码。
What am I missing out?!
我错过了什么?!
采纳答案by jkff
In the <java>
ant task you should add two jvm parameters (<jvmarg>
IIRC) to turn on debugging:
在<java>
ant 任务中你应该添加两个 jvm 参数(<jvmarg>
IIRC)来开启调试:
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5432
This will launch the java program with debugging turned on and the program will be ready to accept debugger connections on port 5432. Then you should use your IDE's remote debugging facility and direct it to connect to port 5432.
这将在调试打开的情况下启动 java 程序,该程序将准备接受端口 5432 上的调试器连接。然后您应该使用 IDE 的远程调试工具并将其引导到端口 5432。
回答by Samuel Lampa
(Wasn't able to comment on the given answer, so have to make another answer)
(无法对给出的答案发表评论,因此必须做出另一个答案)
I realized that when launching Ant from Eclipse, you'll have to add fork="true" to the <java>
task. Also, it was first not clear to me how to write nested jvmargs, so here goes an example:
我意识到从 Eclipse 启动 Ant 时,您必须将 fork="true" 添加到<java>
任务中。另外,我首先不清楚如何编写嵌套的 jvmargs,所以这里有一个例子:
<java classname="..." fork="true">
<jvmarg value="-Xdebug" />
<jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5432" />
...
</java>
回答by zawhtut
This is to help the people who are wondering how to debug the web application that use ant to build and deploy. This is quite frequent in legacy applications. If the project was started as "Dynamic Web Project" as the beginning, following steps and even Ant is not necessary.
这是为了帮助那些想知道如何调试使用 ant 构建和部署的 Web 应用程序的人。这在遗留应用程序中非常常见。如果项目是作为“动态Web项目”开始的,那么下面的步骤甚至Ant都是没有必要的。
Set the break point in your code.
在代码中设置断点。
Window -> Show View -> Others -> Servers
Add your server JBoss or Tomcat for example.
例如,添加您的服务器 JBoss 或 Tomcat。
Right click on the server and choose 'Debug'.
右键单击服务器并选择“调试”。
Make sure that debug="true" is set in ant build file.
确保在 ant 构建文件中设置了 debug="true"。
回答by Ben
This is how I got it working for me(Just commenting for future reference).
这就是我让它为我工作的方式(只是评论以供将来参考)。
Link dump ahead :
提前链接转储:
Debugging ant tasks is not as simple as plain old java debugging. While you can debug an Ant file adding breakpoints, digging inside the code of specific custom task will require you to add a remote debugger in order to be able to "catch" the running process.
I will explain how to do this in Eclipse, altough I recon it can be achieved with all major java IDEs. First thing is to create a new run configuration for the ant file where you plan to use your customized new task. To do so, go to:
Run -> External Tools -> External Tools configuration...
Right click in Ant Build -> New and in the Main tab select your ant script in Buildfile field. Then go to JRE tab and insert the following JVM arguments:
-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n
If you wonder what these arguments mean check this, although a bit updated (Java 1.5) still works.
Once this is done, you must create a new Debug configuration for a remote Java application. To do so, navigate to:
Run-> Debug configurations
Drop down the list in the left column and right click in Remote Java Application -> New. Select the project name in the Project field. Default values for host and port are okay as long as you used the same ones for the Ant configuration (JVM arguments).
Everything is ready for the test run! Add breakpoints wherever you consider necessary. In my case, I added one both in the ant script that uses the custom ant task as well as in the custom ant task, in the execute method.
Right click in your ant script or task -> Debug As...-> Ant >Build first
Now BEFORE calling your custom ant task code, go to Run-> Debug Configurations and debug your previously created Java Remote Application config. This will start a separate thread that will debug your custom ant task code, provided that you included some breakpoints :) You can see in the following image how in my case, thread stopped in the execute method of my custom ant task. After this point, it is up to you to decide what to do next...
调试 ant 任务并不像普通的老式 java 调试那么简单。虽然您可以通过添加断点来调试 Ant 文件,但挖掘特定自定义任务的代码将需要您添加远程调试器,以便能够“捕获”正在运行的进程。
我将解释如何在 Eclipse 中执行此操作,尽管我认为它可以通过所有主要的 Java IDE 实现。首先是为您计划使用自定义新任务的 ant 文件创建一个新的运行配置。为此,请访问:
运行 -> 外部工具 -> 外部工具配置...
右键单击 Ant Build -> New,然后在 Main 选项卡中在 Buildfile 字段中选择您的 ant 脚本。然后转到 JRE 选项卡并插入以下 JVM 参数:
-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n
如果你想知道这些参数是什么意思,请检查这个,虽然有点更新(Java 1.5)仍然有效。
完成此操作后,您必须为远程 Java 应用程序创建新的调试配置。为此,请导航至:
运行->调试配置
下拉左列中的列表并右键单击远程 Java 应用程序 -> 新建。在项目字段中选择项目名称。只要您对 Ant 配置(JVM 参数)使用相同的主机和端口的默认值就可以了。
一切准备就绪,可以进行试运行了!在您认为必要的地方添加断点。就我而言,我在使用自定义 ant 任务的 ant 脚本以及自定义 ant 任务中的 execute 方法中都添加了一个。
右键单击您的 ant 脚本或任务 -> Debug As...-> Ant > Build first
现在在调用您的自定义 ant 任务代码之前,转到运行-> 调试配置并调试您之前创建的 Java 远程应用程序配置。这将启动一个单独的线程来调试您的自定义 ant 任务代码,前提是您包含了一些断点 :) 您可以在下图中看到在我的情况下,线程如何在我的自定义 ant 任务的 execute 方法中停止。在此之后,由您决定下一步要做什么......
回答by abeym
set ANT_OPTS=%ANT_OPTS% -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5432
in Eclipse
在 Eclipse 中
Toolbar >> Run >> Debug Configurations >> + >>
Give the values:
给出值:
Name: Debug_Ant
Project: active-eclipse-project
Host:localhost
Port:5432
回答by KrishPrabakar
In Eclipse:
在日食中:
Toolbar > External Tool Configurations... > (select your existing ANT build or create new) > JRE tab
Toolbar > External Tool Configurations... > (select your existing ANT build or create new) > JRE tab
In "VM Arguments"add:
在“VM 参数”中添加:
-Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y
-Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y
Again Toolbar > Debug > Debug Configurations... > Remote Java Application > New
再次 Toolbar > Debug > Debug Configurations... > Remote Java Application > New
Name: Debug Ant
Project: <Select your project where debug files are kept>
Host: localhost
Port: 8787
Now in "External Tool Configurations" launch "ANT Task" (which waits for the Remote Java Application debugger to connect), then launch the "Debug Ant" from the "Debug" toolbar icon.
现在在“外部工具配置”中启动“ ANT 任务”(等待远程 Java 应用程序调试器连接),然后从“调试”工具栏图标启动“调试 Ant” 。
回答by Soumya Sarkar
Steps:
脚步:
1)Configure remote java debugger with local host as name, port address as 8000(or whatever your system's port address will be)
1)使用本地主机作为名称,端口地址为8000(或任何系统的端口地址)配置远程java调试器
2)Create a batch file and keep that in bin folder of your tomcat(this step is required when we want to debug remotely keeping server/s in same system).
2)创建一个批处理文件并将其保存在您的tomcat的bin文件夹中(当我们要在同一系统中远程调试服务器时需要此步骤)。
in batch file you should keep this line:
在批处理文件中,您应该保留这一行:
set JPDA_ADDRESS=8000
设置 JPDA_ADDRESS= 8000
set JPDA_TRANSPORT=dt_socket
设置 JPDA_TRANSPORT=dt_socket
catalina.bat jpda start
catalina.bat jpda 开始
after this keep a break point in your java code, and debug this remote debugger. it will work.
在这之后在你的java代码中保留一个断点,并调试这个远程调试器。它会起作用。
Happy Coding !!
快乐编码!!
回答by user2346563
I too faced this problem, I did following steps to resolve.
我也遇到了这个问题,我做了以下步骤来解决。
Put the below lines in ANT file
Go to the debugging configurations->Remote java application-> Create new configuration file with project name,port=5432 and host is localhost and save it.
Now run your build.xml using debugging mode, then you should see in console that "Listening for transport dt_socket at address 5432"
Now run debug configuration file which is you configured. Now your selenium code will run using Debug mode.
将以下几行放在 ANT 文件中
进入调试配置->远程java应用程序->创建新的配置文件,项目名称,端口=5432,主机为localhost并保存。
现在使用调试模式运行 build.xml,然后您应该在控制台中看到“在地址 5432 处侦听传输 dt_socket”
现在运行您配置的调试配置文件。现在您的 selenium 代码将使用调试模式运行。
Hope this helps.
希望这可以帮助。
If you still facing issues, please let me know so that i can help you on that.
如果您仍然遇到问题,请告诉我,以便我可以帮助您。
Thanks
谢谢