JavaFX Export and VM arguments

て烟熏妆下的殇ゞ 提交于 2021-01-27 15:01:01

问题


I have the problem that I just can't export my JavaFX application. I can get it running with the VM arguments (inside the IDE and outside) but that's far from optimal. I want a simple "click to open" experience.

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

I am aware that this problem can be fixed with the vm arguments but as I said before "click to open" experience.

I tried to make a fat jar using maven. (Here is my pom.xml):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Test</groupId>
  <artifactId>Test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
   <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-base</artifactId>
            <version>11</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>11</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-media</artifactId>
            <version>11</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-web</artifactId>
            <version>11</version>
        </dependency>
  </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
  <plugins>
       <plugin>
           <artifactId>maven-assembly-plugin</artifactId>
           <version>3.1.1</version>
           <executions>
               <execution>
                   <id>make-jar-with-dependencies</id>
                   <phase>prepare-package</phase>
                   <goals>
                       <goal>single</goal>
                   </goals>
                   <configuration>
                       <archive>
                           <manifest>
                               <mainClass>test.Main</mainClass>
                           </manifest>
                       </archive>
                       <descriptorRefs>
                           <descriptorRef>jar-with-dependencies</descriptorRef>
                       </descriptorRefs>
                   </configuration>
               </execution>
           </executions>
       </plugin>
   </plugins>
 </build>
</project>

I still get the same error but the export process took way longer than usual probably because the JavaFX components were exported too. I think it is worth mentioning that I get a warning in my pom.xml

Overriding managed version 2.2-beta-5 for maven-assembly-plugin pom.xml

Here is my module-info.java (Maybe it helps solving the problem):

module Test {

    requires transitive javafx.controls;
    exports test;

}

回答1:


As I understand the question, a JAR containing the JavaFX dependencies would be an answer.

Using e.g. Maven, it can be created like this:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.1</version>
            <executions>
                <execution>
                    <id>make-jar-with-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>my.group.id.myMain</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Depending on the target platform, you could also wrap the JAR into something using the correct JRE. The launch4j Maven plugins provides further possibilities for this direction.




回答2:


On Windows, it is possible to solve the problem by editing the registry. This is by no means a perfect solution, but it does allow you to click-open JavaFX thin jars, which is a major benefit.

Using Regedit, find HKEY_CLASSES_ROOT\jar_auto_file\shell\open\command and change its data field to:

PATH_TO_JAVAW --module-path PATH_TO_JAVAFX_LIB --add-modules=javafx.controls -jar "%1"

where:

  • PATH_TO_JAVAW is the path to javaw.exe
  • PATH_TO_JAVAFX_LIB is the path to the lib directory of JavaFX



回答3:


Okay. After some fiddling around and some research I am glad to say that I finally found a solution. My problem with maven was that I compiled with eclipse and did not build with maven. So here is my final pom.xml for my project:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Project-Zola</groupId>
  <artifactId>Project-Zola</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>11</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-graphics </artifactId>
        <version>11</version>
        <classifier>win</classifier>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-graphics</artifactId>
        <version>11</version>
        <classifier>linux</classifier>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-graphics </artifactId>
        <version>11</version>
        <classifier>mac</classifier>
    </dependency>

    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml </artifactId>
        <version>11</version>
        <classifier>win</classifier>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>11</version>
        <classifier>linux</classifier>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml </artifactId>
        <version>11</version>
        <classifier>mac</classifier>
    </dependency>

  </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <release>12</release>
        </configuration>
      </plugin>
      <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>exec-maven-plugin</artifactId>
         <version>1.6.0</version>
         <executions>
             <execution>
                 <goals>
                     <goal>java</goal>
                 </goals>
             </execution>
         </executions>
         <configuration>
             <mainClass>at.dominik.zola.main.Launcher</mainClass>
         </configuration>
     </plugin>
     <plugin>
         <artifactId>maven-jar-plugin</artifactId>
         <configuration>
             <archive>
                 <manifest>
                     <mainClass>
                         at.dominik.zola.main.Launcher
                     </mainClass>
                 </manifest>
             </archive>
         </configuration>
     </plugin>
     <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-shade-plugin</artifactId>
         <version>3.2.0</version>
         <executions>
             <execution>
                 <phase>package</phase>
                 <goals>
                     <goal>shade</goal>
                 </goals>
                 <configuration>
                     <transformers>
                         <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                             <mainClass>at.dominik.zola.main.Launcher</mainClass>
                         </transformer>
                     </transformers>
                 </configuration>
             </execution>
         </executions>
     </plugin>
    </plugins>
  </build>
</project>

The jar is executable now and I have to give props to Erik Nellessen because he already submitted a similar answer. This pom.xml is also cross-platform compatible. But be careful if you are using the JavaFX web stuff you need to also include the JavaFX web components in the pom. I generally do not recommend that because the jar would then also include WebKit components and its size would increase by like 200mb. Thank's to all the people who tried to help me :)

NOTE: It is important that your main class does not extend the Application class that's why I created a Laucher class wich just calls the main method in the actual main class.



来源:https://stackoverflow.com/questions/58936763/javafx-export-and-vm-arguments

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