I have my own build.xml
file consisting of few targets to compile and run my Java project with Ant. Here is the relevant part of this one:
<path id="libpath">
<fileset dir="${lib.dir}" includes="**/*.jar" />
</path>
<patternset id="resources">
<include name="relative/path/to/resources/" />
</patternset>
<path id="resourcespath">
<fileset dir="${src.dir}">
<patternset refid="resources" />
</fileset>
</path>
<target name="compile">
<mkdir dir="${build.dir}" />
<mkdir dir="${classes.dir}" />
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="libpath" includeantruntime="false" debug="true" debuglevel="lines,vars,source" />
<copy todir="${classes.dir}">
<path refid="resourcespath" />
</copy>
</target>
<target name="debug" depends="compile">
<java fork="true" classname="${main-class}">
<sysproperty key="java.library.path" path="${dist.dir}"/>
<classpath>
<pathelement location="${classes.dir}" />
<path refid="libpath" />
</classpath>
</java>
</target>
All I want to do is to debug the compiled code in ${classes.dir}
using Netbeans by running the debug
target of build.xml
. At the moment, this target starts the compiled application without giving a chance to stop at breakpoints. I know that Netbeans generates default build-impl.xml
file but this one is too large and is difficult to understand for me. That's why I want to know whether it's possible to use the Netbeans IDE to debug Java code compiled by Ant using my own build.xml
file.
There is also the similar question on debugging Java code when using Ant script in Eclipse, but as I can see the solution proposed is Eclipse specific.
Method proposed by Sergey works but one have to do the following actions each time to debug a project:
Ctrl+F5
), Netbeans will wait for a debbuger to attach;Shift+F5
will not terminate the application as usual, it will only finish the debugging session).In my opinion, the better way to use "Attach Debugger..." is to choose SocketListen
connector:
jvmarg
's suboption server
must be set to n
- refer to Remote Debugging FAQ).Ctrl+F5
).Shift+F5
.Anyway, this method is also uncomfortable, especially if you were accustomed to start the debugging by simply pressing Ctrl+F5
or pressing corresponding toolbar button. Choosing required debugger from the dropdown menu everytime is annoying.
So here is the best solution - start the debugger directly from Ant's debug
target. In my case it looks like this:
<target name="debug" depends="compile">
<nbjpdastart addressproperty="jpda.address" name="MyProjectName" transport="dt_socket">
<classpath>
<pathelement location="${classes.dir}" />
<path refid="libpath" />
</classpath>
</nbjpdastart>
<java fork="true" classname="${main-class}">
<sysproperty key="java.library.path" path="${dist.dir}" />
<classpath>
<pathelement location="${classes.dir}" />
<path refid="libpath" />
</classpath>
<jvmarg value="-Xdebug" />
<jvmarg value="-Xnoagent" />
<jvmarg value="-Djava.compiler=none" />
<jvmarg value="-Xrunjdwp:transport=dt_socket,address=${jpda.address}" />
</java>
</target>
To get more information, please, look at Creating a Target to Debug Your Java SE Application
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With