I can't debug an application using netbeans 11 with JavaFX 12

僤鯓⒐⒋嵵緔 提交于 2019-11-28 12:43:49

If you see the documentation of the javafx-maven-plugin, you can add some VM arguments to the run goal in order to debug your project in NetBeans.

However, to keep the usual run goal ready to just run the project and not debug, without commenting out the added options, we can add a second execution to the plugin.

Modify your plugin like this:

<plugin>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>0.0.2</version>
    <executions>
        <execution>
            <!-- Default configuration for running -->
            <id>default-cli</id>
            <configuration>
                <mainClass>com.mycompany.simonsaysgfx.App</mainClass>
            </configuration>
        </execution>
        <execution>
            <!-- Configuration for debugging -->
            <id>debug</id>
            <configuration>
                <options>
                    <option>-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:8000</option>
                </options>
                <mainClass>com.mycompany.simonsaysgfx.App</mainClass>
            </configuration>
        </execution>
    </executions>
</plugin>

Now you can run from command line:

mvn clean javafx:run

to run as usual your application, and:

mvn clean javafx:run@debug

to start debug mode. Then you will see something like:

[INFO] --- javafx-maven-plugin:0.0.2:run (debug) @ Project ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /path/to/project/target/classes
Listening for transport dt_socket at address: 8000

At this point, you need to set your breakpoints and attach a debugger from NetBeans -> Debug -> Attach Debugger to port 8000:

Click OK and you will be able to debug your projects.

Note that you can also define custom NetBeans actions to use the Run and Debug buttons. Add a nbactions.xml file to the root of your project, with this two actions:

<?xml version="1.0" encoding="UTF-8"?>
<actions>
    <action>
        <actionName>run</actionName>
        <goals>
            <goal>clean</goal>
            <goal>javafx:run</goal>
        </goals>
    </action>
    <action>
        <actionName>jlink</actionName>
        <goals>
            <goal>clean</goal>
            <goal>javafx:jlink</goal>
        </goals>
    </action>
    <action>
        <actionName>debug</actionName>
        <goals>
            <goal>clean</goal>
            <goal>javafx:run@debug</goal>
        </goals>
    </action>
</actions>

Now you can use NetBeans run and debug buttons.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!