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

3

6

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.

Unstop answered 9/10, 2018 at 15:46 Comment(5)
Use modular java application.Brothers
@mrmcwolf But it's possible to use JavaFX 11 in a non modualr application (openjfx.io/openjfx-docs/#install-javafx). So why do I have to use a modular one?Unstop
Debugging from the exec:java goal (Maven Projects window, right click -> Debug [exec:java]), works for me, without adding the SDK.Compte
@JoséPereda Thanks. It works for me too. But maybe you have any thoughts why a configuration of type Application doesn't work (the configuration type you've suggested is Maven)? It has always worked for my previous Maven projects.Unstop
@Dfm I am running into the same problem. I created another run configuration: exec:java -f pom.xml.This fails with the following error: The parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java are missing or invalid. If I add a plugin configuration with the mainclass, I get this error: The specified mainClass doesn't contain a main method with appropriate signature.Randall
K
3

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 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! :)

Kilohertz answered 28/1, 2020 at 4:41 Comment(0)
W
0

It helped me to create a module-info.java file in the src/main/java folder that looks like this:

module HelloFX {
    requires javafx.controls;
    exports hellofx; //package name where the main class is
}

image

Washrag answered 15/10 at 15:23 Comment(0)
T
-1

If the point is just to use the run/debug button you can use a Maven Configuration maven config for intelliJ JavaFX run

Trinitrophenol answered 15/3, 2019 at 21:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.