How to fix “Error: JavaFX runtime components are missing, and are required to run this application”

倾然丶 夕夏残阳落幕 提交于 2019-12-02 07:25:37

问题


My JavaFx application is running perfectly well from source but When I'm compiling to a single jar file I get error :

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

I'm using Maven as my repository manager and My install with Maven is sucessfull.

Note: In my Intellij build artifact I can see that Intellij include JavaFx and all its libraries


回答1:


a) For my maven project the trick was to use an extra starter class that does not inherit from javafx.application.Application:

public class GUIStarter {

    public static void main(final String[] args) {
        GUI.main(args);
    }

}

The JavaFx dependencies are included in my pom.xml as follows:

<!-- JavaFx -->
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-base</artifactId>
            <version>12</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>12</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics </artifactId>
            <version>12</version>
            <classifier>win</classifier>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>12</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-web</artifactId>
            <version>12</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-media</artifactId>
            <version>12</version>
        </dependency>

         <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-swing</artifactId>
            <version>12</version>
        </dependency>

In order to include fxml files in your build, you might need to define them as resource:

!-- define JavaFx files as resources to include them in the jar -->
<resource>
      <directory>${basedir}/src/main/java/foo/baa/view</directory>
      <filtering>false</filtering>  
      <targetPath>foo/baa/view</targetPath>            
</resource>

b) As an alternative, an extra maven plugin "javafx-maven-plugin" could be used to build the javafx appication, see example project at https://openjfx.io/openjfx-docs/#maven (This did not work for me. I have several pom files and want to reference the javafx dependencies in a sub project. My main class could not be found by the javafx-maven-plugin.)




回答2:


In Java 11 the JavaFX components have been removed into their own SDK, so when you run it won't be able to find them in the runtime.

The JavaFX page has the instructions to get it going: https://openjfx.io/openjfx-docs/#install-javafx

In short, you have to compile / run by adding the javafx modules in, either as options passed on the command line, or using a modular project.

I found that the modular project with Maven and IntelliJ worked best for me. https://openjfx.io/openjfx-docs/#IDE-Intellij

In the modular method you have a module-info.java file that describes all the modules that your project "requires", and allows you to "open" them to other modules. If you have a bunch of Maven dependencies you have to add them in the requires list too though. (IntelliJ can make this easy - find the error about no requires in the code and alt-enter)

Once everything was working with modules etc, I had to make a Fat Jar using I think the Maven shade plugin, to put everything together. Then it would work running the jar from the command line.

However, after getting my Java 11 code @#$%@# working after 2 days of pain, I went back to Java 8 (using correto SDK for latest version) as Java 11 doesn't have packaging and IntelliJ can't do it for you.



来源:https://stackoverflow.com/questions/56894627/how-to-fix-error-javafx-runtime-components-are-missing-and-are-required-to-ru

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