Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Netbeans to debug the code compiled using Ant's custom build.xml?

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.

like image 224
ezze Avatar asked Dec 08 '11 07:12

ezze


1 Answers

Method proposed by Sergey works but one have to do the following actions each time to debug a project:

  1. Press Debug Main Project button (Ctrl+F5), Netbeans will wait for a debbuger to attach;
  2. Choose "Debug" > "Attach Debugger..." from Netbeans main menu, specify debugger's parameters in appeared dialog and then press "OK" button or select previously used debbuger from dropdown list of toolbar's debug button (e.g. Attach to 5432);
  3. Debug your application and then close it (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:

  1. Perform step 2 from the previous steps' chain (select Listen to 5432) - debugger will wait for an application to start (please note, that jvmarg's suboption server must be set to n - refer to Remote Debugging FAQ).
  2. Press Debug Main Project button (Ctrl+F5).
  3. Debug your application and then close it or terminate it simultaneously with debugging session by pressing 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

like image 163
ezze Avatar answered Nov 10 '22 23:11

ezze