How to execute JAVA FX 11 JAR without providing VM

2020-03-27 06:57发布

Java : JDK 12
Build Tool : Maven
IDE : Eclipse
OS : Windows

I have a simple piece of java FX 11 code which displays a simple blank screen. I have made deployed an executable jar using eclipse. It works fine when i give the following command using CMD:

CMD command to run jar

As it is visible that i need to provide the modules at time of execution of JAR file.

If we skip this step we get JAR direct execution error:

JAR direct execution error

As I have already tried using maven as :

---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>com.proj1</groupId>
    <artifactId>Proj1</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <type>maven-plugin</type>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>13-ea+7</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                <compilerArgs>
                <arg>--add modules</arg><arg> javafx.controls,javafx.fxml,javafx.graphics</arg>
                </compilerArgs>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.1</version>
                <configuration>
                    <mainClass>org.openjfx.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

But even when this is done the exported executable JAR still demands the arguments.

Is it possible to somehow avoid this through CMD and make the JAR executable by simply double clicking it using Maven.

I am not asking on how to solve the javaFx runtime exception but on how to solve it by adding dependencies so that when the JAR is distributed the client does not have to pass the runtime arguments and get the job done by simple clicks.

1条回答
在下西门庆
2楼-- · 2020-03-27 07:32

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>
查看更多
登录 后发表回答