简体   繁体   English

使用Maven的JavaFX与exec:java一起使用,但在运行时不在jar中

[英]JavaFX with Maven works with exec:java but not in the jar at runtime

I would like to execute a jar with JavaFX on any platform, whatever JavaFX is installed or not on the host, using maven. 我想在任何平台上使用JavaFX执行jar,无论JavaFX是否安装在主机上,都使用maven。

This is working as long as I run my class with 只要我运行我的课程,这是有效的

mvn exec:java

However, I can't produce a jar with dependencies working. 但是,我无法生成一个依赖于工作的jar。 At runtime, I got the following (it compiles) 在运行时,我得到以下(它编译)

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

In my pom, I have the following: 在我的pom中,我有以下内容:

<dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-controls</artifactId>
      <version>11</version>
</dependency>
...
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
...
      <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>Gui</mainClass>
        </configuration>
</plugin>
<plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <mainClass>Gui</mainClass>
            </manifest>
          </archive>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id> 
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
</plugin>

So I don't get why it works with exec:java (so meaning that the dependencies are OK) but not via the jar... 所以我不明白为什么它适用于exec:java(所以意味着依赖是正常的)但不是通过jar ...

As pointed in comment by José Pereda, this answer solved it. 正如JoséPereda的评论所指出的, 这个答案解决了它。 To be short, I solved the problem by creating a class extending JavaFX's Application but not making this class as the main class in the jar. 简而言之,我通过创建一个扩展JavaFX的Application的类来解决这个问题,但是没有将这个类作为jar中的主类。 Instead, I call the main method of the extended Application into my real main class. 相反,我将扩展Application的main方法调用到我的真实主类中。

Example: 例:

public class RealMain { //the class in the manifest
    public static void main(String[] args) {
        Gui.main(args);
    }
}

public class Gui extends Application{
    public static void main(String[] args) {
        launch();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM