Running .jar with CLI

huangapple go评论77阅读模式
英文:

Running .jar with CLI

问题

我正在尝试导出一个可运行的.jar文件,但我面临以下问题:

“VM参数将不包含在可运行的JAR中。参数可以在启动JAR时通过命令行传递。”

我忽略了警告并点击“完成”以创建可运行的.jar文件。但当我双击它时,它不起作用。

我在命令行中运行了以下代码:

java -jar C:\path\file.jar --module-path "C:\pathtofxsdk11\lib" --add-modules javafx.controls,javafx.fxml,javafx.base,javafx.swing,javafx.graphics

之后,我收到以下错误:

图形设备初始化失败:d3d, sw
初始化QuantumRenderer时出错:找不到合适的管道
java.lang.RuntimeException: java.lang.RuntimeException: 初始化QuantumRenderer时出错:找不到合适的管道
在com.sun.javafx.tk.quantum.QuantumRenderer.getInstance(QuantumRenderer.java:280)处
在com.sun.javafx.tk.quantum.QuantumToolkit.init(QuantumToolkit.java:222)处
在com.sun.javafx.tk.Toolkit.getToolkit(Toolkit.java:260)处
在com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:267)处
在com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:158)处
在com.sun.javafx.application.LauncherImpl.startToolkit(LauncherImpl.java:658)处
在com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:678)处
在com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)处
在java.base/java.lang.Thread.run(Thread.java:830)处
由于java.lang.RuntimeException引发:初始化QuantumRenderer时出错:找不到合适的管道
在com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.init(QuantumRenderer.java:94)处
在com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:124)处
... 1更多
“主”线程中的异常java.lang.reflect.InvocationTargetException
在java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)处
在java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)处
在java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)处
在java.base/java.lang.reflect.Method.invoke(Method.java:567)处
在org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61)处
由于java.lang.RuntimeException引发:找不到工具包
在com.sun.javafx.tk.Toolkit.getToolkit(Toolkit.java:272)处
在com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:267)处
在com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:158)处
在com.sun.javafx.application.LauncherImpl.startToolkit(LauncherImpl.java:658)处
在com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:678)处
在com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)处
在java.base/java.lang.Thread.run(Thread.java:830)处
我需要帮助创建可执行的Jar文件。

<details>
<summary>英文:</summary>

I am trying to export a runnable .jar file however I am faced with the following problems:

 &quot;VM arguments will not be part of the runnable JAR. Arguments can be passed on the command line when launching the JAR&quot;

I ignored the warning and hit finish to create the runnable .jar file. When I double click it doesn&#39;t work.

I ran the following code in my command line:

    **java -jar C:\path\file.jar --module-path &quot;C:\pathtofxsdk11\lib&quot;  --add-modules javafx.controls,javafx.fxml,javafx.base,javafx.swing, javafx.graphics**

After which, I received the following error:

    Graphics Device initialization failed for :  d3d, sw
    Error initializing QuantumRenderer: no suitable pipeline found
    java.lang.RuntimeException: java.lang.RuntimeException: Error initializing QuantumRenderer: no suitable pipeline found
            at com.sun.javafx.tk.quantum.QuantumRenderer.getInstance(QuantumRenderer.java:280)
            at com.sun.javafx.tk.quantum.QuantumToolkit.init(QuantumToolkit.java:222)
            at com.sun.javafx.tk.Toolkit.getToolkit(Toolkit.java:260)
            at com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:267)
            at com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:158)
            at com.sun.javafx.application.LauncherImpl.startToolkit(LauncherImpl.java:658)
            at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:678)
            at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
            at java.base/java.lang.Thread.run(Thread.java:830)
    Caused by: java.lang.RuntimeException: Error initializing QuantumRenderer: no suitable pipeline found
            at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.init(QuantumRenderer.java:94)
            at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:124)
            ... 1 more
    Exception in thread &quot;main&quot; java.lang.reflect.InvocationTargetException
            at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.base/java.lang.reflect.Method.invoke(Method.java:567)
            at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61)
    Caused by: java.lang.RuntimeException: No toolkit found
            at com.sun.javafx.tk.Toolkit.getToolkit(Toolkit.java:272)
            at com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:267)
            at com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:158)
            at com.sun.javafx.application.LauncherImpl.startToolkit(LauncherImpl.java:658)
            at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:678)
            at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
            at java.base/java.lang.Thread.run(Thread.java:830)

I need help with creating the executable Jar.




</details>


# 答案1
**得分**: 0

提供的命令行选项顺序不正确。在 jar 文件的名称之后(或者如果您没有使用 `-jar` 选项,则在主类名称之后)提供的任何内容都将被解释为传递给您的 Java 应用程序的参数(而不是传递给 JVM 的参数),并传递给主类中的字符串数组中的 `public static void main(String[])` 方法。

因此,您应该使用以下命令:

```shell
java --module-path "C:\pathtofxsdk11\lib" --add-modules javafx.controls,javafx.fxml,javafx.base,javafx.swing,javafx.graphics -jar C:\path\file.jar

请注意,这并不意味着 jar 文件可以用作“可执行的 jar”,可以在任何系统上简单运行,因为从版本 11 开始,JavaFX 运行时不包括在标准 Java 运行时中。相反,您可以使用 JDK 14 中包含的 jpackage 工具来创建本地安装程序包。此包将包括一个Java运行时,您可以配置该运行时以包括JavaFX。

首先,您应该从 这里 下载并解压缩 JavaFX 的模块化版本("jmods")。

然后,您的 jpackage 命令应该类似于以下内容:

jpackage --module-path "C:\pathtofxmods" --add-modules javafx.controls,javafx.fxml,javafx.swing --input "C:\path" --main-jar file.jar --type exe --name MyApp --dest "C:\path\to\generated\executable"

其中 C:\pathtofxmods 是您在前一步中解压缩的库:它应该包括一组 *.jmod 文件。这将在 C:\path\to\generated\executable 中生成一个 .exe 文件,该文件将在 Windows 系统上安装应用程序。您可以分发此文件 - 请注意,最终用户甚至不需要 JRE,因为该文件将一个 JRE 与包一起打包。

您还可以独立运行 jlink 来创建与应用程序捆绑在一起的包括 JavaFX 的 JRE。请查看完整教程完整工具文档

英文:

The options you provided in the command line are in the wrong order. Anything supplied after the name of the jar file (or the main class, if you are not using the -jar option) will be interpreted as an argument to your Java application (as opposed to an argument to the JVM), and passed to the string array in the

public static void main(String[])

method in the main class.

Thus you should use

java --module-path &quot;C:\pathtofxsdk11\lib&quot;  --add-modules javafx.controls,javafx.fxml,javafx.base,javafx.swing,javafx.graphics -jar C:\path\file.jar

Note that this doesn't mean the jar file can be used as an "executable jar" that can simply be run on any system, because the JavaFX runtime is not included in the standard Java runtime from version 11 onwards. Instead, you can use the jpackage tool, which is included in JDK 14, to create a native installer bundle. This bundle will include a Java runtime, which you can configure to include JavaFX.

You should first download and unzip the modular version of JavaFX (the "jmods") from here.

Then your jpackage command looks something like the following:

jpackage --module-path &quot;C:\pathtofxmods&quot; --add-modules javafx.controls,javafx.fxml,javafx.swing --input &quot;C:\path&quot; --main-jar file.jar --type exe --name MyApp --dest &quot;C:\path\to\generated\executable&quot;

where C:\pathtofxmods is the library you unzipped in the previous step: it should include a collection of *.jmod files. This will generate an .exe file in C:\path\to\generated\executable which will install the application on a windows system. You can distribute this - note the end users do not even need a JRE, as this will bundle a JRE with the package.

You can also run jlink independently to create the JRE (with JavaFX) that is bundled with the application. See a full tutorial and the full tool documentation.

huangapple
  • 本文由 发表于 2020年8月6日 22:40:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63286025.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定