admin管理员组

文章数量:1312982

I want to learn JavaFX development, but first, got to ensure the application can be exported and run on its own, so it can be shared with regular users in the future - preferably Linux and Apple aswell, but Windows (my OS) only is also fine.

Tried pretty much everything I can think of, but I always get an error about something not found, existing, or whatever, when running the non-exported project in CommandPrompt (except the list of commands that the IDEs use). When exporting the application from Eclipse as Runnable JAR, and doubleclicking the .jar, either nothing happens or a "java exception has occured rip" error popup happens. The only ways that successfully run the application aren't very end-user-friendly, and don't look that clean to run like everyday apps do, which the goal is to fix.

I primarily use Eclipse, but also did IntelliJ to narrow down the issue.

Content - step by step what I've done and tried, with what happens etc.

  • Preparation

    • Java version
    • Environment Variables
    • Java SDK folders
  • Creating the projects

    • Eclipse, modular and non modular
    • IntelliJ
  • Running the projects - not exported

    • Eclipse, modular and non modular

      • In IDE (works)

      • In CommandPrompt, short/simple commands (doesnt work)

      • In CommandPrompt, confusing command used by Eclipse (works)

    • IntelliJ

      • (the same as eclipse)
  • Exporting/deploying the projects

    • Eclipse

    • intelliJ

  • Running the exported/deployed projects

    • Made by Eclipse
      • As .jar, through Java Platform SE (doesnt work)

      • In CommandPrompt (works)

      • In CommandPrompt (shows the .jar error)

    • Made by IntelliJ
      • Using app.bat file (works)

      • Changing app.zip to app.jar, and running with Java Platform SE (doesnt work)

Preparation


Using Java 8. Downloaded from java. Though java --version in CommandPrompt gives Java SE Runtime Environment build 23.0.1+11-39

Environment Variables

  • "JAVA_HOME" = "C:\Program Files\Java\jdk-23"
  • "PATH_TO_FX" = "C:\Program Files\Java\javafx-sdk-23.0.1\lib"
  • "Path" = "C:\Program Files\Java\jdk-23\bin"

Java folders

Java.
├───javafx-sdk-23.0.1
│   ├───bin
│   ├───legal
│   └───lib
├───jdk-23
│   ├───bin
│   ├───conf
│   ├───include
│   ├───jmods
│   ├───legal
│   └───lib
├───jre1.8.0_431
│   ├───bin
│   ├───legal
│   └───lib
├───latest
│   └───jre-1.8
│       ├───bin
│       ├───legal
│       └───lib
└───openjfx-23.0.1_windows-x64_bin-jmods
    └───javafx-jmods-23.0.1

Creating the projects


Eclipse - non modular

  1. File > New > Other > JavaFX Project

  2. Give a name > "Use default JRE 'jdk-23' ..." > Selecting "Create module-info.java file" or not creates it anyway > Finish

  3. Delete the "module-info.java" file > Rightclick project folder > Run As > Run Configurations > Arguments > In "VM arguments" enter --module-path "%PATH_TO_FX%" --add-modules javafx.controls,javafx.fxml (%PATH_TO_FX% uses the Environment Variable) > Apply

  4. Rightclick project folder > Build Path > Configure Build Path > Remove "JavaFX SDK" (it's the wrong JavaFX library) > Add Library > User Library > Select the downloaded JavaFX library (mine is "JavaFX 23.0.1") > Apply > Apply and close

Directory tree for non modular

non_modular_test:.
│   .classpath
│   .project
│   build.fxbuild
│
├───.settings
│       .eclipse.core.resources.prefs
│
├───bin
│   └───application
│           application.css
│           Main.class
│
└───src
    └───application
            application.css
            Main.java

Main.java code

package application;
    
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

Eclipse - modular

  1. Same as step 1-2 for non modular

  2. Same as step 4 for non modular

Directory tree for modular

modular_test:.
│   .classpath
│   .project
│   build.fxbuild
│
├───.settings
│       .eclipse.core.resources.prefs
│
├───bin
│   │   module-info.class
│   │
│   └───application
│           application.css
│           Main.class
│
└───src
    │   module-info.java
    │
    └───application
            application.css
            Main.class
            Main.java

Main.java code

Same as non modular

IntelliJ

  1. (home screen) New Project > JavaFX > Give a name > Just go with Maven > Select JDK > Next

  2. Select the libraries you want included (just for making the app run, i dont use any) > Create

Directory tree for IntelliJ

demo:.
│   .gitignore
│   mvnw
│   mvnw.cmd
│   pom.xml
│
├───.idea
│       .gitignore
│       compiler.xml
│       encodings.xml
│       jarRepositories.xml
│       misc.xml
│       workspace.xml
│
├───.mvn
│   └───wrapper
│           maven-wrapper.jar
│           maven-wrapper.properties
│
├───src
│   └───main
│       ├───java
│       │   │   module-info.java
│       │   │
│       │   └───com
│       │       └───example
│       │           └───demo
│       │                   HelloApplication.java
│       │                   HelloController.java
│       │
│       └───resources
│           └───com
│               └───example
│                   └───demo
│                           hello-view.fxml
│
└───target
    ├───classes
    │   │   module-info.class
    │   │
    │   └───com
    │       └───example
    │           └───demo
    │                   hello-view.fxml
    │                   HelloApplication.class
    │                   HelloController.class
    │
    └───generated-sources
        └───annotations

HelloApplication.java code

package com.example.demo;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 320, 240);
        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

Running the projects - not exported


Eclipse

  • Non modular and modular - in IDE

    1. Rightclick project folder > Run As > Java Application

    2. Select Main java file > OK

    3. Runs without a sweat

  • Non modular - in CommandPrompt. Short/simple commands

    1. In CommandPrompt, navigate to main java file's folder, using cd path\to\file

    2. Type javac --module-path "%PATH_TO_FX%" --add-modules javafx.controls,javafx.fxml Main.java

    3. Type java --module-path "%PATH_TO_FX%" --add-modules javafx.controls,javafx.fxml Main

    4. Get error: Error: Could not find or load main class Main Caused by: java.lang.NoClassDefFoundError: Main (wrong name: application/Main)

    5. Nvm try java Main.java (works with standard java code, when only using JRE library)

    6. Get new error: (kept short)

      Main.java:3: error: package javafx.application does not exist
      import javafx.application.Application;
                               ^
      Main.java:4: error: package javafx.stage does not exist
      import javafx.stage.Stage;
                         ^
      
    • Another error when going straight for java (modules bla bla) Main.java , but there's enough details already
  • Non modular - in CommandPrompt. Confusing command used by Eclipse

    1. In CommandPrompt, type the following command:

      (everything in 1 command. The new lines are solely for lil readability, so replace with spaces)

      (Eclipse includes some UTF-8 commands. Idk if they add functionality, but not needed for app to run)

      "C:\Program Files\Java\jdk-23\bin\javaw.exe"
      --module-path "%PATH_TO_FX%"
      --add-modules javafx.controls,javafx.fxml
      -p "C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.base.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.controls.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.fxml.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.graphics.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.media.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.swing.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.web.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx-swt.jar"
      -classpath "C:\Users\[user]\eclipse-workspace\non_modular_test\bin" application.Main
      
    2. App runs (yay)

  • Modular - in CommandPrompt. Short/simple commands

    1. Same as non modular
    • (except java (modules bla bla) Main.java error is different. Still already enough details)
  • Modular - in CommandPrompt. Confusing command used by Eclipse

    1. Do the same as non modular, but tiny changes:

      "C:\Program Files\Java\jdk-23\bin\javaw.exe"
      -p "C:\Users\[user]\eclipse-workspace\modular_test\bin;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.base.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.controls.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.fxml.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.graphics.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.media.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.swing.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.web.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx-swt.jar"
      -m modular_test/application.Main
      
    2. App runs

IntelliJ

  • In IDE

    1. Press Run button

    2. It runs

  • In CommandPrompt, short/simple commands

    1. Same as Eclipse
  • In CommandPrompt, confusing command used by IntelliJ

    1. Enter the following command in CommandPrompt:

      (again, everything in 1 command. Multiple lines here for readability)

      (IntelliJ also uses some UTF-8 commands, tho not needed to run)

      "C:\Program Files\Java\jdk-23\bin\java.exe"
      "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.3.1.1\lib\idea_rt.jar=52330:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.3.1.1\bin"
      -classpath "C:\Users\[user]\.m2\repository\\openjfx\javafx-controls\17.0.6\javafx-controls-17.0.6.jar;C:\Users\[user]\.m2\repository\\openjfx\javafx-graphics\17.0.6\javafx-graphics-17.0.6.jar;C:\Users\[user]\.m2\repository\\openjfx\javafx-base\17.0.6\javafx-base-17.0.6.jar;C:\Users\[user]\.m2\repository\\openjfx\javafx-fxml\17.0.6\javafx-fxml-17.0.6.jar"
      -p "C:\Users\[user]\.m2\repository\\openjfx\javafx-controls\17.0.6\javafx-controls-17.0.6-win.jar;C:\Users\[user]\.m2\repository\\openjfx\javafx-fxml\17.0.6\javafx-fxml-17.0.6-win.jar;C:\Users\[user]\IdeaProjects\demo2\target\classes;C:\Users\[user]\.m2\repository\\openjfx\javafx-graphics\17.0.6\javafx-graphics-17.0.6-win.jar;C:\Users\[user]\.m2\repository\\openjfx\javafx-base\17.0.6\javafx-base-17.0.6-win.jar"
      -m com.example.demo2/com.example.demo2.HelloApplication
      
    2. App runs

Exporting/deploying the projects


Eclipse

  1. Rightclick project folder or click File in upper left corner > Export > Runnable JAR

  2. Select project > Select export destination (as .jar - browse button lets you go with .zip aswell) > Select either Extract or Package (they're different, as shown later) > Finish

As non modular, there is a warning on top of the export menu that says: "VM arguments will not be part of the runnable JAR. Arguments can be passed on the command line when launching the JAR", though practically this warning doesn't matter (see chapter about running the exports)

  • Modular and non modular, extracted libraries

Tree (shortened)

extract:.
├───application
│   ├───application.css
│   └───Main.class
├───com
│   └───sun
├───javafx
│   ├───animation
│   ├───application
│   ├───beans
│   ├───collections
│   ├───concurrent
│   ├───css
│   ├───embed
│   ├───event
│   ├───fxml
│   ├───geometry
│   ├───print
│   ├───scene
│   ├───stage
│   └───util
└───META-INF
    └───substrate
        └───config

MANIFEST.MF

Manifest-Version: 1.0
Main-Class: application.Main
Class-Path: .
  • Modular and non modular, packaged libraries

Tree (little shortened). Non modular doesn't have module-info.class

modular_package:.
│   javafx-swt.jar
│   javafx.base.jar
│   javafx.controls.jar
│   javafx.fxml.jar
│   javafx.graphics.jar
│   javafx.media.jar
│   javafx.swing.jar
│   javafx.web.jar
│   module-info.class
│
├───application
│       application.css
│       Main.class
│
├───META-INF
│       MANIFEST.MF
│
└───
    └───eclipse

MANIFEST.MF

Manifest-Version: 1.0
Main-Class: .eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
Rsrc-Class-Path: ./ javafx.base.jar javafx.controls.jar javafx.fxml.jar 
 javafx.graphics.jar javafx.media.jar javafx.swing.jar javafx.web.jar ja
 vafx-swt.jar
Rsrc-Main-Class: application.Main
Class-Path: .

IntelliJ

  • Can't use artifacts for .jar packaging, cuz thats only for JDK 8

  • Packaging with jlink

    1. Doubleclick CTRL

    2. For maven, type mvn javafx:jlink

    3. The export is the "target" folder in the project

Tree (shortened. theres also an "app.zip", identical to app folder, but doesnt show in tree)

target:.
├───app
│   ├───bin
│   │   ├───app (file)
│   │   ├───app.bat
│   │   ├───java.dll
│   │   ├───java.exe
│   │   ├───javaw.exe
│   │   └───server
│   │       └───jvm.dll
│   ├───conf
│   │   └───security
│   │       └───policy
│   │           ├───limited
│   │           └───unlimited
│   ├───legal
│   │   ├───java.base
│   │   ├───java.datatransfer
│   │   ├───java.desktop
│   │   ├───java.prefs
│   │   ├───java.scripting
│   │   ├───java.xml
│   │   └───jdk.unsupported
│   └───lib
│       └───security
├───classes
│   └───com
│       └───example
│           └───demo
├───generated-sources
│   └───annotations
└───maven-status
    └───maven-compiler-plugin
        └───compile
            └───default-compile

app.bat

@echo off
set JLINK_VM_OPTIONS=
set DIR=%~dp0
"%DIR%\java" %JLINK_VM_OPTIONS% -m com.example.demo/com.example.demo.HelloApplication %*

Running the exported/deployed projects


Made with Eclipse

  • Rightclicking and running with Java (TM) Platform SE. Modular and non modular is the same

    • Extracted libraries: Java Exception error popup

    • Packaged libraries: Nothing happens

  • Running in CommandPrompt (works for all)

    1. Navigate to desktop folder

    2. Enter java --module-path "%PATH_TO_FX%" --add-modules javafx.controls,javafx.fxml -jar app.jar

    3. It runs fine

    • Only problem is that CommandPrompt must stay open, or application closes with it
  • Running in CommandPrompt (errors showing why .jar doesn't work). Modular and non modular is still the same

    • Navigate to desktop folder > Enter java -jar app.jar

    • Extracted libraries:

      Error: JavaFX runtime components are missing, and are required to run this application
      
    • Packaged libraries: (only the unnamed module is different - also error is shortened)

      [date of running] com.sun.javafx.application.PlatformImpl startup
      
      WARNING: Unsupported JavaFX configuration: classes were loaded from 'unnamed module @[hexcode]'
      
      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
      
      Caused by: java.lang.RuntimeException: Error initializing QuantumRenderer: no suitable pipeline found
      
      Exception in thread "main" java.lang.reflect.InvocationTargetException
      
      Caused by: java.lang.RuntimeException: No toolkit found
      

Made with IntelliJ

  • Running through app.bat file

    1. Navigate to app.bat file, in "bin" folder

    2. Doubleclick the file

    3. CommandPrompt opens, and app runs fine

    • Only problem is that CommandPrompt must stay open, or application closes with it

    • Rightclicking the "app" file, and opening with Java Platform SE, will briefly open CommandPrompt and nothing more happens

  • Changing "app.zip" folder, to "app.jar" and running

    1. Rightclick the folder > Open with > Java Platform SE

    2. Get error: "invalid or corrupt jarfile :("

    • Same result if java -jar app.jar in CommandPrompt

I want to learn JavaFX development, but first, got to ensure the application can be exported and run on its own, so it can be shared with regular users in the future - preferably Linux and Apple aswell, but Windows (my OS) only is also fine.

Tried pretty much everything I can think of, but I always get an error about something not found, existing, or whatever, when running the non-exported project in CommandPrompt (except the list of commands that the IDEs use). When exporting the application from Eclipse as Runnable JAR, and doubleclicking the .jar, either nothing happens or a "java exception has occured rip" error popup happens. The only ways that successfully run the application aren't very end-user-friendly, and don't look that clean to run like everyday apps do, which the goal is to fix.

I primarily use Eclipse, but also did IntelliJ to narrow down the issue.

Content - step by step what I've done and tried, with what happens etc.

  • Preparation

    • Java version
    • Environment Variables
    • Java SDK folders
  • Creating the projects

    • Eclipse, modular and non modular
    • IntelliJ
  • Running the projects - not exported

    • Eclipse, modular and non modular

      • In IDE (works)

      • In CommandPrompt, short/simple commands (doesnt work)

      • In CommandPrompt, confusing command used by Eclipse (works)

    • IntelliJ

      • (the same as eclipse)
  • Exporting/deploying the projects

    • Eclipse

    • intelliJ

  • Running the exported/deployed projects

    • Made by Eclipse
      • As .jar, through Java Platform SE (doesnt work)

      • In CommandPrompt (works)

      • In CommandPrompt (shows the .jar error)

    • Made by IntelliJ
      • Using app.bat file (works)

      • Changing app.zip to app.jar, and running with Java Platform SE (doesnt work)

Preparation


Using Java 8. Downloaded from java. Though java --version in CommandPrompt gives Java SE Runtime Environment build 23.0.1+11-39

Environment Variables

  • "JAVA_HOME" = "C:\Program Files\Java\jdk-23"
  • "PATH_TO_FX" = "C:\Program Files\Java\javafx-sdk-23.0.1\lib"
  • "Path" = "C:\Program Files\Java\jdk-23\bin"

Java folders

Java.
├───javafx-sdk-23.0.1
│   ├───bin
│   ├───legal
│   └───lib
├───jdk-23
│   ├───bin
│   ├───conf
│   ├───include
│   ├───jmods
│   ├───legal
│   └───lib
├───jre1.8.0_431
│   ├───bin
│   ├───legal
│   └───lib
├───latest
│   └───jre-1.8
│       ├───bin
│       ├───legal
│       └───lib
└───openjfx-23.0.1_windows-x64_bin-jmods
    └───javafx-jmods-23.0.1

Creating the projects


Eclipse - non modular

  1. File > New > Other > JavaFX Project

  2. Give a name > "Use default JRE 'jdk-23' ..." > Selecting "Create module-info.java file" or not creates it anyway > Finish

  3. Delete the "module-info.java" file > Rightclick project folder > Run As > Run Configurations > Arguments > In "VM arguments" enter --module-path "%PATH_TO_FX%" --add-modules javafx.controls,javafx.fxml (%PATH_TO_FX% uses the Environment Variable) > Apply

  4. Rightclick project folder > Build Path > Configure Build Path > Remove "JavaFX SDK" (it's the wrong JavaFX library) > Add Library > User Library > Select the downloaded JavaFX library (mine is "JavaFX 23.0.1") > Apply > Apply and close

Directory tree for non modular

non_modular_test:.
│   .classpath
│   .project
│   build.fxbuild
│
├───.settings
│       .eclipse.core.resources.prefs
│
├───bin
│   └───application
│           application.css
│           Main.class
│
└───src
    └───application
            application.css
            Main.java

Main.java code

package application;
    
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

Eclipse - modular

  1. Same as step 1-2 for non modular

  2. Same as step 4 for non modular

Directory tree for modular

modular_test:.
│   .classpath
│   .project
│   build.fxbuild
│
├───.settings
│       .eclipse.core.resources.prefs
│
├───bin
│   │   module-info.class
│   │
│   └───application
│           application.css
│           Main.class
│
└───src
    │   module-info.java
    │
    └───application
            application.css
            Main.class
            Main.java

Main.java code

Same as non modular

IntelliJ

  1. (home screen) New Project > JavaFX > Give a name > Just go with Maven > Select JDK > Next

  2. Select the libraries you want included (just for making the app run, i dont use any) > Create

Directory tree for IntelliJ

demo:.
│   .gitignore
│   mvnw
│   mvnw.cmd
│   pom.xml
│
├───.idea
│       .gitignore
│       compiler.xml
│       encodings.xml
│       jarRepositories.xml
│       misc.xml
│       workspace.xml
│
├───.mvn
│   └───wrapper
│           maven-wrapper.jar
│           maven-wrapper.properties
│
├───src
│   └───main
│       ├───java
│       │   │   module-info.java
│       │   │
│       │   └───com
│       │       └───example
│       │           └───demo
│       │                   HelloApplication.java
│       │                   HelloController.java
│       │
│       └───resources
│           └───com
│               └───example
│                   └───demo
│                           hello-view.fxml
│
└───target
    ├───classes
    │   │   module-info.class
    │   │
    │   └───com
    │       └───example
    │           └───demo
    │                   hello-view.fxml
    │                   HelloApplication.class
    │                   HelloController.class
    │
    └───generated-sources
        └───annotations

HelloApplication.java code

package com.example.demo;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 320, 240);
        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

Running the projects - not exported


Eclipse

  • Non modular and modular - in IDE

    1. Rightclick project folder > Run As > Java Application

    2. Select Main java file > OK

    3. Runs without a sweat

  • Non modular - in CommandPrompt. Short/simple commands

    1. In CommandPrompt, navigate to main java file's folder, using cd path\to\file

    2. Type javac --module-path "%PATH_TO_FX%" --add-modules javafx.controls,javafx.fxml Main.java

    3. Type java --module-path "%PATH_TO_FX%" --add-modules javafx.controls,javafx.fxml Main

    4. Get error: Error: Could not find or load main class Main Caused by: java.lang.NoClassDefFoundError: Main (wrong name: application/Main)

    5. Nvm try java Main.java (works with standard java code, when only using JRE library)

    6. Get new error: (kept short)

      Main.java:3: error: package javafx.application does not exist
      import javafx.application.Application;
                               ^
      Main.java:4: error: package javafx.stage does not exist
      import javafx.stage.Stage;
                         ^
      
    • Another error when going straight for java (modules bla bla) Main.java , but there's enough details already
  • Non modular - in CommandPrompt. Confusing command used by Eclipse

    1. In CommandPrompt, type the following command:

      (everything in 1 command. The new lines are solely for lil readability, so replace with spaces)

      (Eclipse includes some UTF-8 commands. Idk if they add functionality, but not needed for app to run)

      "C:\Program Files\Java\jdk-23\bin\javaw.exe"
      --module-path "%PATH_TO_FX%"
      --add-modules javafx.controls,javafx.fxml
      -p "C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.base.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.controls.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.fxml.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.graphics.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.media.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.swing.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.web.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx-swt.jar"
      -classpath "C:\Users\[user]\eclipse-workspace\non_modular_test\bin" application.Main
      
    2. App runs (yay)

  • Modular - in CommandPrompt. Short/simple commands

    1. Same as non modular
    • (except java (modules bla bla) Main.java error is different. Still already enough details)
  • Modular - in CommandPrompt. Confusing command used by Eclipse

    1. Do the same as non modular, but tiny changes:

      "C:\Program Files\Java\jdk-23\bin\javaw.exe"
      -p "C:\Users\[user]\eclipse-workspace\modular_test\bin;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.base.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.controls.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.fxml.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.graphics.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.media.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.swing.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx.web.jar;C:\Program Files\Java\javafx-sdk-23.0.1\lib\javafx-swt.jar"
      -m modular_test/application.Main
      
    2. App runs

IntelliJ

  • In IDE

    1. Press Run button

    2. It runs

  • In CommandPrompt, short/simple commands

    1. Same as Eclipse
  • In CommandPrompt, confusing command used by IntelliJ

    1. Enter the following command in CommandPrompt:

      (again, everything in 1 command. Multiple lines here for readability)

      (IntelliJ also uses some UTF-8 commands, tho not needed to run)

      "C:\Program Files\Java\jdk-23\bin\java.exe"
      "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.3.1.1\lib\idea_rt.jar=52330:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.3.1.1\bin"
      -classpath "C:\Users\[user]\.m2\repository\\openjfx\javafx-controls\17.0.6\javafx-controls-17.0.6.jar;C:\Users\[user]\.m2\repository\\openjfx\javafx-graphics\17.0.6\javafx-graphics-17.0.6.jar;C:\Users\[user]\.m2\repository\\openjfx\javafx-base\17.0.6\javafx-base-17.0.6.jar;C:\Users\[user]\.m2\repository\\openjfx\javafx-fxml\17.0.6\javafx-fxml-17.0.6.jar"
      -p "C:\Users\[user]\.m2\repository\\openjfx\javafx-controls\17.0.6\javafx-controls-17.0.6-win.jar;C:\Users\[user]\.m2\repository\\openjfx\javafx-fxml\17.0.6\javafx-fxml-17.0.6-win.jar;C:\Users\[user]\IdeaProjects\demo2\target\classes;C:\Users\[user]\.m2\repository\\openjfx\javafx-graphics\17.0.6\javafx-graphics-17.0.6-win.jar;C:\Users\[user]\.m2\repository\\openjfx\javafx-base\17.0.6\javafx-base-17.0.6-win.jar"
      -m com.example.demo2/com.example.demo2.HelloApplication
      
    2. App runs

Exporting/deploying the projects


Eclipse

  1. Rightclick project folder or click File in upper left corner > Export > Runnable JAR

  2. Select project > Select export destination (as .jar - browse button lets you go with .zip aswell) > Select either Extract or Package (they're different, as shown later) > Finish

As non modular, there is a warning on top of the export menu that says: "VM arguments will not be part of the runnable JAR. Arguments can be passed on the command line when launching the JAR", though practically this warning doesn't matter (see chapter about running the exports)

  • Modular and non modular, extracted libraries

Tree (shortened)

extract:.
├───application
│   ├───application.css
│   └───Main.class
├───com
│   └───sun
├───javafx
│   ├───animation
│   ├───application
│   ├───beans
│   ├───collections
│   ├───concurrent
│   ├───css
│   ├───embed
│   ├───event
│   ├───fxml
│   ├───geometry
│   ├───print
│   ├───scene
│   ├───stage
│   └───util
└───META-INF
    └───substrate
        └───config

MANIFEST.MF

Manifest-Version: 1.0
Main-Class: application.Main
Class-Path: .
  • Modular and non modular, packaged libraries

Tree (little shortened). Non modular doesn't have module-info.class

modular_package:.
│   javafx-swt.jar
│   javafx.base.jar
│   javafx.controls.jar
│   javafx.fxml.jar
│   javafx.graphics.jar
│   javafx.media.jar
│   javafx.swing.jar
│   javafx.web.jar
│   module-info.class
│
├───application
│       application.css
│       Main.class
│
├───META-INF
│       MANIFEST.MF
│
└───
    └───eclipse

MANIFEST.MF

Manifest-Version: 1.0
Main-Class: .eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
Rsrc-Class-Path: ./ javafx.base.jar javafx.controls.jar javafx.fxml.jar 
 javafx.graphics.jar javafx.media.jar javafx.swing.jar javafx.web.jar ja
 vafx-swt.jar
Rsrc-Main-Class: application.Main
Class-Path: .

IntelliJ

  • Can't use artifacts for .jar packaging, cuz thats only for JDK 8

  • Packaging with jlink

    1. Doubleclick CTRL

    2. For maven, type mvn javafx:jlink

    3. The export is the "target" folder in the project

Tree (shortened. theres also an "app.zip", identical to app folder, but doesnt show in tree)

target:.
├───app
│   ├───bin
│   │   ├───app (file)
│   │   ├───app.bat
│   │   ├───java.dll
│   │   ├───java.exe
│   │   ├───javaw.exe
│   │   └───server
│   │       └───jvm.dll
│   ├───conf
│   │   └───security
│   │       └───policy
│   │           ├───limited
│   │           └───unlimited
│   ├───legal
│   │   ├───java.base
│   │   ├───java.datatransfer
│   │   ├───java.desktop
│   │   ├───java.prefs
│   │   ├───java.scripting
│   │   ├───java.xml
│   │   └───jdk.unsupported
│   └───lib
│       └───security
├───classes
│   └───com
│       └───example
│           └───demo
├───generated-sources
│   └───annotations
└───maven-status
    └───maven-compiler-plugin
        └───compile
            └───default-compile

app.bat

@echo off
set JLINK_VM_OPTIONS=
set DIR=%~dp0
"%DIR%\java" %JLINK_VM_OPTIONS% -m com.example.demo/com.example.demo.HelloApplication %*

Running the exported/deployed projects


Made with Eclipse

  • Rightclicking and running with Java (TM) Platform SE. Modular and non modular is the same

    • Extracted libraries: Java Exception error popup

    • Packaged libraries: Nothing happens

  • Running in CommandPrompt (works for all)

    1. Navigate to desktop folder

    2. Enter java --module-path "%PATH_TO_FX%" --add-modules javafx.controls,javafx.fxml -jar app.jar

    3. It runs fine

    • Only problem is that CommandPrompt must stay open, or application closes with it
  • Running in CommandPrompt (errors showing why .jar doesn't work). Modular and non modular is still the same

    • Navigate to desktop folder > Enter java -jar app.jar

    • Extracted libraries:

      Error: JavaFX runtime components are missing, and are required to run this application
      
    • Packaged libraries: (only the unnamed module is different - also error is shortened)

      [date of running] com.sun.javafx.application.PlatformImpl startup
      
      WARNING: Unsupported JavaFX configuration: classes were loaded from 'unnamed module @[hexcode]'
      
      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
      
      Caused by: java.lang.RuntimeException: Error initializing QuantumRenderer: no suitable pipeline found
      
      Exception in thread "main" java.lang.reflect.InvocationTargetException
      
      Caused by: java.lang.RuntimeException: No toolkit found
      

Made with IntelliJ

  • Running through app.bat file

    1. Navigate to app.bat file, in "bin" folder

    2. Doubleclick the file

    3. CommandPrompt opens, and app runs fine

    • Only problem is that CommandPrompt must stay open, or application closes with it

    • Rightclicking the "app" file, and opening with Java Platform SE, will briefly open CommandPrompt and nothing more happens

  • Changing "app.zip" folder, to "app.jar" and running

    1. Rightclick the folder > Open with > Java Platform SE

    2. Get error: "invalid or corrupt jarfile :("

    • Same result if java -jar app.jar in CommandPrompt
Share Improve this question edited Feb 2 at 16:01 Federico klez Culloca 27.2k17 gold badges59 silver badges101 bronze badges asked Feb 2 at 15:58 MutraxMutrax 336 bronze badges 10
  • Applications using JavaFX are no different from any other Java application. – mr mcwolf Commented Feb 2 at 16:04
  • 1 You're clearly frustrated and tried many ways. I've voted to close as you need to strip it back to focus on the problem: if you are not able achieve your goal just using steps directly from your chosen JDK with javac/jar/jlink/java commands, then it is pointless showing how it also fails with Maven, Eclipse, and IntellJ. I suggest you use javac --source-path / -d targetdir, jar on the target dir, jlink to make a runtime image that contains JavaFX, then "java -jar your.jar" using that image should work without more command line arguments. – DuncG Commented Feb 2 at 17:12
  • 1 @DuncG could you explain it a little more detailed? tried the javac sourcepath and target dir command, but not sure how to correctly use it. Also, i went really deep with the entire process of attempts in the post, because in previous post (deleted now), I did try focusing more on the actual problem, but question was closed due to lack of desired behavior and specified problem. Thought I had described the desired behavior well enough, and did my best with explaining the problem with how little i knew about it. Post remained closed despite editing it to include more details that might help – Mutrax Commented Feb 2 at 17:43
  • 1 If you're willing to consider jlink and/or jpackage to create the deployed executable, a complete example is examined here and the related answers are informative. – trashgod Commented Feb 2 at 18:15
  • 1 Start here. – SedJ601 Commented Feb 2 at 18:42
 |  Show 5 more comments

1 Answer 1

Reset to default 1

As mentioned in my comment, focus entirely on running with your chosen JDK with a compatible JavaFX. These steps are for a non-module project, and later you should make your project use modules and phase out the --class-path parts.

Setup

Make sure that your JDK is on your path, such that java --version prints expected version, and you can run javac/jlink/jar in similar manner. In Windows where java.exe should reference jdk-XX\bin\java.exe folder.

Open a CommandPrompt. Set a variable for the path to JavaFX SDK, and one for a JavaFX JMODS. NOTE: jlink needs the JavaFX JMOD release, though you use JavaFX SDK release for compile and run.

set JFXHOME=C:\java\javafx-sdk-YY
set JFXMOD=C:\java\javafx-jmods-YY

Compile

Run compiler, note that I've specified source-path and full path to the Main.java.

javac --module-path "%JFXHOME%/lib" --add-modules javafx.controls,javafx.fxml --source-path src -d targetdir src\application\Main.java

Copy any resources into your target folder from src:

copy src\application\application.css targetdir\application\application.css

Test

This should run your application using targetdir as classpath, note the full package name is needed:

java --module-path "%JFXHOME%/lib" --add-modules javafx.controls,javafx.fxml --class-path targetdir application.Main

Create JRE with JavaFX

Check that you have JavaFX JMOD release, this should print names of various jmod files:

dir %JFXMOD%\*.jmod

Package a runtime that includes these JavaFX jmods:

jlink --module-path "%JFXMOD%" --add-modules javafx.controls,javafx.fxml --output myjre

Test classpath with JRE including JavaFX

No need for JavaFX parameters if you run your apps in the combined JRE with JavaFX:

myjre\bin\java --class-path targetdir application.Main

Build a runnable jar

jar -c -f app.jar --main-class application.Main -C targetdir .

Test runnable jar with JRE including JavaFX

myjre\bin\java -jar app.jar

Next steps

Once you understand what is needed, hopefully you may find it easier to get your chosen IDE to help. Preferably look at Ant, Maven or Gradle so that you won't ever need to do the manual steps again for any project you have.

Ideally, switch to using module-info for your own projects.

You can use jpackage to make installer for the project (module or non-module version)

本文标签: