How to execute JAVA FX 11 JAR without providing VM args via CMD

本小妞迷上赌 提交于 2019-12-02 05:32:34

With the JavaFX maven plugin you can execute two goals: run and jlink. The former will just run the project with the required arguments (--module-path, --add-modules), so you can run on command line:

mvn clean javafx:run

Of course, this is not intended for distribution.

javafx:jlink

However, if your project is modular (i.e you have a module-info.java file), you can set your plugin like:

<plugin>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>0.0.2</version>
    <configuration>
        <mainClass>hellofx/org.openjfx.App</mainClass>
        <launcher>app</launcher>
        <jlinkImageName>appDir</jlinkImageName>
        <jlinkZipName>appZip</jlinkZipName>
    </configuration>
</plugin>

and run:

mvn clean javafx:jlink

It will generate a custom runtime image with your project that you can distribute, and you can add a launcher or even zip it. Once extracted you will only need this to run it:

target/appdir/app

See the plugin options here.

Shade plugin

You can also use the maven-shade-plugin.

As explained here you will need a main class that doesn't extend from Application:

Launcher.java

package org.openjfx;

public class Launcher {

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

And now you can add the shade plugin to your pom:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.1</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation=
                                         "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>org.openjfx.Launcher</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

Run mvn clean package, and it will generate your fat jar that you can distribute and run as:

java -jar target/hellofx-1.0-SNAPSHOT.jar

Cross platform

Note that in both cases (jlink or shade plugin), you will have a jar that you can distribute only to run on the same platform as yours.

However you can make it multiplaform if you include the dependencies for other platforms as well:

<dependency>
  <groupId>org.openjfx</groupId>
  <artifactId>javafx-controls</artifactId>
  <version>12.0.1</version>
</dependency>
<dependency>
  <groupId>org.openjfx</groupId>
  <artifactId>javafx-fxml</artifactId>
  <version>12.0.1</version>
</dependency>
<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-graphics</artifactId>
    <version>12.0.1</version>
    <classifier>win</classifier>
</dependency>
<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-graphics</artifactId>
    <version>12.0.1</version>
    <classifier>linux</classifier>
</dependency>
<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-graphics</artifactId>
    <version>12.0.1</version>
    <classifier>mac</classifier>
</dependency>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!