Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a JavaFX 11 Maven project in IntelliJ IDEA using a run/debug configuration

I want to run a HelloWorld JavaFX 11 application using Maven from IntelliJ IDEA using a run/debug configuration to be able to debug the application.

I've created a Maven project in IntelliJ IDEA with pom.xml and HelloFX.java. I can build the project successfully and run it executing the specified compile and exec:java goals.

But when I run it using a run/debug configuration with VM options --add-modules=javafx.controls, I get:

Error occurred during initialization of boot layer
java.lang.module.FindException: Module javafx.controls not found

The only way I've managed to run it is by downloading a JavaFX runtime and setting VM options to --module-path /path/to/javafx-sdk-11/lib --add-modules=javafx.controls.

But is there any way to run it using artifacts downloaded from Maven? As I've thought that IntelliJ IDEA can use Maven artifacts in runtime. Or am I just getting something wrong?

P.S. I am aware about this answer, but it doesn't describe what I want to do.

like image 392
DfM Avatar asked Oct 09 '18 15:10

DfM


People also ask

How do I get to the run debug configuration in IntelliJ?

From the main menu, select Run | Edit Configurations. Alternatively, press Alt+Shift+F10 , then 0 . on the toolbar or press Alt+Insert . The list shows the run/debug configuration templates.


1 Answers

I think I may have finally figured out a solution.

I'm assuming you're using Maven to import the javafx-maven-plugin as well as javafx-controls dependency, just as I was trying to do (and, ultimately, jona303's answer DIDN'T fix), and that you've had issues/an annoyance trying to run it using javafx:run.

Well, the solution, without using --add-exports and other things, is actually fairly simple. Create an extra class (I called mine "Launcher") with a proper, Java-based main method. Then, in the class, launch your application from there, like so:

Launcher Class

public class Launcher {

    public static void main(String[] args) {
        Application.launch(App.class, args);
    }

}

Application Class

public class App extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        // app code
    }

}

I'm pretty sure the issue is caused by IntelliJ trying to be "smart", and launching an application without a main method, jumping directly to the start method. But by doing that, it somehow avoids adding the Maven dependencies and causes it to fail. When you do it this way, it forces IntelliJ to say "okay we need the maven dependencies, then we can run it", properly launching it (because it doesn't know any better that it's a JavaFX project). But that's just a guess. All I know is that it works for me.

It took me absolutely forever to figure this out, but once I set my run configuration to launch using my Launcher class instead of my App class, everything worked seamlessly like a charm (even debugging worked!).

Hopefully this helps, and happy coding! :)

like image 166
FireController1847 Avatar answered Oct 12 '22 07:10

FireController1847