maven - 如何在不通过 CMD 提供 VM args 的情况下执行 JAVA FX 11 JAR
问题描述
Java:JDK 12
构建工具:Maven
IDE:Eclipse
操作系统:Windows
我有一段简单的 java FX 11 代码,它显示一个简单的空白屏幕。我已经使用 eclipse 部署了一个可执行的 jar。当我使用 CMD 发出以下命令时,它工作正常:
可见我需要在执行 JAR 文件时提供模块。
如果我们跳过这一步,我们会得到 JAR 直接执行错误:
正如我已经尝试使用 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>
但即使这样做了,导出的可执行 JAR 仍然需要参数。
是否有可能通过 CMD 以某种方式避免这种情况,并通过使用 Maven 双击它来使 JAR 可执行。
我不是在问如何解决 javaFx 运行时异常,而是在问如何通过添加依赖项来解决它,以便在分发 JAR 时客户端不必传递运行时参数并通过简单的单击完成工作。
解决方案
使用 JavaFX maven 插件,您可以执行两个目标:run
和jlink
. 前者只会使用所需的参数 ( --module-path
, --add-modules
) 运行项目,因此您可以在命令行上运行:
mvn clean javafx:run
当然,这不是为了分发。
javafx:jlink
但是,如果您的项目是模块化的(即您有一个module-info.java
文件),您可以将插件设置为:
<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
它将使用您可以分发的项目生成自定义运行时映像,您可以添加启动器甚至压缩它。提取后,您只需要它来运行它:
target/appdir/app
请参阅此处的插件选项。
阴影插件
您也可以使用maven-shade-plugin
.
正如这里所解释的,您将需要一个不扩展自的主类Application
:
启动器.java
package org.openjfx;
public class Launcher {
public static void main(String[] args) {
App.main(args);
}
}
现在您可以将阴影插件添加到您的 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
,它将生成您可以分发和运行的胖 jar:
java -jar target/hellofx-1.0-SNAPSHOT.jar
跨平台
请注意,在这两种情况下(jlink 或 shade 插件),您都会有一个 jar,您只能分发它以在与您相同的平台上运行。
但是,如果您还包含其他平台的依赖项,则可以使其成为 multiplaform:
<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>
推荐阅读
- macos - 在 Tomcat 上部署 Web 服务调用后,Tomcat 崩溃
- go - 如何注释它们在其他项目中共享的符号?
- javascript - 根据字符串数组重新排序对象数组?
- java - 如何在 Spring Data ChangeStreamOptions.builder().filter(arg0) 中使用 MongoDB Aggregations JSON String 作为参数“arg0”
- ios - 具有拖动手势的单元格使用 SwiftUI 覆盖列表滚动
- jquery - 使用 jQuery 和数据属性值填充选择选项
- java - 如何从相机中检索照片以进行gridview?
- wordpress - 主题未呈现
- java - Hybris中testing.xml中定义的testing_additional_tests.txt有什么用?
- python - 如何摆脱python中赋值运算符的SettingWithCopyWarning?