如何在不通过CMD提供VM参数的情况下执行JAVA FX 11 JAR [英] How to execute JAVA FX 11 JAR without providing VM args via CMD

查看:105
本文介绍了如何在不通过CMD提供VM参数的情况下执行JAVA FX 11 JAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java:JDK 12
生成工具:Maven
IDE:Eclipse
操作系统:Windows

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

我有一个简单的Java FX 11代码,它显示了一个简单的空白屏幕. 我已经使用eclipse部署了一个可执行jar. 当我使用CMD发出以下命令时,它工作正常:

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:

可见,我需要在执行JAR文件时提供模块.

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

如果我们跳过此步骤,则会得到JAR直接执行错误:

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

因为我已经尝试过使用maven作为:

As I have already tried using maven as :

--- Maven pom.xml

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

但是即使这样做,导出的可执行文件JAR仍然需要参数.

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

是否有可能通过CMD避免这种情况,并只需使用Maven双击即可使JAR可执行.

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

我不是在问如何解决javaFx运行时异常,而是在问如何通过添加依赖项来解决它,以便在分发JAR时,客户端不必传递运行时参数即可通过单击来完成工作.

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.

推荐答案

使用JavaFX maven插件,您可以执行两个目标:runjlink.前者将仅使用所需的参数(--module-path--add-modules)运行项目,因此您可以在命令行上运行:

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

但是,如果您的项目是模块化的(即您有一个module-info.java文件),则可以将插件设置为:

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>

并运行:

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

此处中查看插件选项.

阴影插件

您也可以使用maven-shade-plugin.

此处所述,您将需要一个不包含以下内容的主类:不能从Application扩展:

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);
    }
}

现在您可以将阴影插件添加到pom:

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>

运行mvn clean package,它将生成您的胖子罐,您可以将其分发并以以下方式运行:

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

跨平台

请注意,在两种情况下(jlink或shade插件),您都将拥有一个jar,只能分发该jar才能在与您相同的平台上运行.

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>

这篇关于如何在不通过CMD提供VM参数的情况下执行JAVA FX 11 JAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆