JavaFX 导出和 VM 参数

JavaFX Export and VM arguments

我遇到了无法导出 JavaFX 应用程序的问题。我可以获得 运行 VM 参数(在 IDE 内部和外部),但这远非最佳。我想要一个简单的 "click to open" 体验。

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

我知道这个问题可以用 vm 参数解决,但正如我之前所说 "click to open" 经验。

我尝试使用 maven 制作一个 fat jar。 (这是我的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>

我仍然遇到同样的错误,但导出过程花费的时间比平时长,可能是因为 JavaFX 组件也被导出了。 我认为值得一提的是,我在 pom.xml

中收到警告

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

这是我的模块-info.java(也许对解决问题有帮助):

module Test {

    requires transitive javafx.controls;
    exports test;

}

根据我的理解,一个包含 JavaFX 依赖项的 JAR 将是一个答案。

使用例如Maven,可以这样创建:

<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>

根据目标平台,您还可以使用正确的 JRE 将 JAR 包装到某些东西中。 launch4j Maven 插件为这个方向提供了更多可能性。

好的。经过一些摆弄和一些研究,我很高兴地说我终于找到了解决方案。我对 maven 的问题是我用 eclipse 编译,而不是用 maven 构建。所以这是我项目的最终 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>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>

jar 现在可以执行了,我必须支持 Erik Nellessen,因为他已经提交了类似的答案。这个 pom.xml 也是跨平台兼容的。但是要小心,如果你使用的是 JavaFX web 东西,你还需要在 pom.xml 中包含 JavaFX web 组件。我通常不建议这样做,因为 jar 也会包含 WebKit 组件,并且它的大小会增加大约 200mb。感谢所有试图帮助我的人:)

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.