javaeclipsejavafxjarjava-13

Running .jar with CLI


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

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

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

I ran the following code in my command line:

**java -jar C:\path\file.jar --module-path "C:\pathtofxsdk11\lib"  --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 "main" 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.


Solution

  • 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 "C:\pathtofxsdk11\lib"  --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 "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"
    

    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.