2

I used the Intellij JavaFX generator to create a JavaFx application with Java17 and Maven. The javafx-maven-plugin specifies a main class and I can run the app from this main class. But I also want to run the app from a test class. If I try that I get this error:

Error: JavaFX runtime components are missing, and are required to run this application

Originally I built the app with Java8 and JavaFX. With Java8 I could run the app from the main class as well as the test class. Simply converting the project to Java17 didn't work so I had Intellij generate a new project and copied the old source into the new project. With Java17 I can run the app from the main class but not from the test class.

How can I run the app from the test class in the new project? Thanks in advance!

Edit:

An excerpt from the pom as generated by Intellij:

        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.8</version>
            <executions>
                <execution>
                    <!-- Default configuration for running with: mvn clean javafx:run -->
                    <id>default-cli</id>
                    <configuration>
                        <mainClass>io.wouter.gmsbooks.offerapp/io.wouter.gmsbooks.GmsOfferApp</mainClass>
                        <launcher>app</launcher>
                        <jlinkZipName>app</jlinkZipName>
                        <jlinkImageName>app</jlinkImageName>
                        <noManPages>true</noManPages>
                        <stripDebug>true</stripDebug>
                        <noHeaderFiles>true</noHeaderFiles>
                    </configuration>
                </execution>
            </executions>
        </plugin>

The javafx-maven-plugin only mentions a main class, no test class. Or should I add another execution referring to the test class?

wouterio
  • 137
  • 1
  • 10
  • 1
    With java8, javafx is part of the jdk, but no longer the case for java17. You may have to add javafx module path and modules to your java command line parameters. It would be something like this: --module-path $your-javafx-sdk-path --add-modules javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web – user9035826 Aug 04 '23 at 14:42
  • (A) In your POM, add a dependency for the OpenJFX libraries that implement JavaFX. Or else use a JDK from Azul Systems or BellSoft that bundle the libraries. (B) JavaFX is [modularized](https://en.wikipedia.org/wiki/Java_Platform_Module_System). You’ll need to learn the basics of JPMS, and add a `module-info.java` file. – Basil Bourque Aug 04 '23 at 16:10
  • 2
    How are you runnng the app with JavaFX 17 when it works and when it breaks? What is are command lines used? Edit the question and add the commands there as text, formatted as code. Provide a [mcve] to replicate the issue. Idea new JavaFX project will add a module-info which makes your app modular, but perhaps your app doesn’t function as a modular app, and you *may* need to follow non-modular getting started instructions at openjfx.io Easiest though might be to use a JDK that includes JavaFX as suggested by Basil, it might work with your existing project with no additional changes. – jewelsea Aug 04 '23 at 17:27
  • Perhaps a duplicate: [ntelliJ can't recognize JavaFX 11 with OpenJDK 11](https://stackoverflow.com/questions/52467561/intellij-cant-recognize-javafx-11-with-openjdk-11) – jewelsea Aug 04 '23 at 17:35
  • @CjY I'm able to run the app from the main class without any command line params. The problems is only when I run the app from a test class. I'll update the question. – wouterio Aug 04 '23 at 19:12
  • @BasilBourque The dependencies are already included in the pom, and I already have the module-info.java file in place. Since I'm able to run the app from the main class, my guess would be that the pom and module files are ok. – wouterio Aug 04 '23 at 19:15
  • @jewelsea The app works when I run it from the main class, it breaks when I run it from a test class. The main class and test class are identical except that the test class instantiates a mock endpoints. I'm quite sure that this difference is not the problem. – wouterio Aug 04 '23 at 19:19
  • When your main class runs, what is your java command line parameters look like? While it's running, you can do something like this to capture it: wmic path win32_process where "name like '%java%'" get Caption,Processid,Commandline – user9035826 Aug 04 '23 at 19:49
  • "_Or should I add another execution referring to the test class?_" – That would seem the correct solution to me (but I'm more familiar with Gradle than Maven). – Slaw Aug 04 '23 at 20:37
  • Also note, that if you are in the IDE and the project is correctly configured and setup, there is no need to use the `javafx-maven-plugin` at all, as demoed in the [Idea new project wizard documentation](https://www.jetbrains.com/help/idea/javafx.html). Unless you are using the plugin for linking (I don't think you are), it doesn't need to be in your project. – jewelsea Aug 04 '23 at 20:47
  • Please add the requested execution commands to the question as text, formatted as code. You can get them from the Idea console when you run the app. It will show up in the console as a grayed-out string like `/java . . .` Click on the `. . .`, it will expand the line to show the entire execution command. Copy and paste that into the question as text formatted as code. Do it for the execution that works, and the one that does not. You can't be helped if nobody really knows what you are doing, and it cannot be replicated in another environment. A [mcve] is best. – jewelsea Aug 05 '23 at 04:05
  • [openjfx.io](https://openjfx.io/openjfx-docs/) also documents use of JavaFX. – jewelsea Aug 05 '23 at 04:07
  • 1
    @CjY Ok so in the end I just went for your `--module-path --add-modules` approach. Still don't understand why first the main class was running and the test class not... But with this approach both I can run both classes. All of you thanks for your help! – wouterio Aug 07 '23 at 12:27

0 Answers0