8

I am stuck at a very basic problem. I have created a simple hello world program using JavaFX which works fine on JDK 1.8. But when I switch to JDK-11 it throws following exception:

Error: Could not find or load main class application.Main
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application

Following is the code I wrote in eclipse.

package application;

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


public class Main extends Application {

    private Scene theScene;
    @Override
    public void start(Stage primaryStage) {
try {

            FXMLLoader loader = new FXMLLoader(getClass().getResource("MyScene.fxml"));
            Parent mainPane = loader.load();


            theScene = new Scene(mainPane);
            primaryStage.setScene(theScene);
            primaryStage.show();

        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void setTheScene(Scene theScene) {
        this.theScene = theScene;
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Naman
  • 27,789
  • 26
  • 218
  • 353
Shahrukh S.
  • 81
  • 1
  • 1
  • 3
  • Where and how are you running it (In eclipse/Intellij, as JAR, etc)? – Gnas Nov 15 '18 at 12:21
  • Possible duplicate of [JavaFX packaging: NoClassDefFoundError](https://stackoverflow.com/questions/52761836/javafx-packaging-noclassdeffounderror) – The Head Rush Nov 15 '18 at 12:26
  • I am running jar file using "java -jar myapp.jar" – Shahrukh S. Nov 15 '18 at 12:26
  • How did you build the jar? – Gnas Nov 15 '18 at 12:30
  • Java 11 doest not support javafx thats why you are getting this error. – GauravRai1512 Nov 15 '18 at 12:31
  • @TheHeadRush I want that my app should be able to run on both. java 8 and Java 11. Well I found on internet and it says that JavaFX is not the port of JDK11. Should I need to modify the app by adding external JavaFX libraries and recompile it? – Shahrukh S. Nov 15 '18 at 12:34
  • 3
    Java 11 doesn't _include_ JavaFX 11, but that doesn't mean it doesn't support it. You can run JavaFX 11 apps, and there is a getting started guide [here](https://openjfx.io/openjfx-docs/). – José Pereda Nov 15 '18 at 12:35
  • @Gnas I build it using Eclipse: Exporting as Runable Jar file – Shahrukh S. Nov 15 '18 at 12:35
  • Are you building it with JDK 1.8 and running on JDK 11? Because that won't work due to the removal of JavaFX since JDK 11. From JDK 11 onwards you need to explicitly include JavaFX dependencies as you would any other third party libraries. – Gnas Nov 15 '18 at 12:39
  • Thanks @Gnas. I included explicitly all java dependencies and I think it should now work. It is showing following error: Error: JavaFX runtime components are missing, and are required to run this application I think there is a very minor thing that I am missing my be there is something I have to add in environment variable. Actually I am new to Java that's why asking this stupid question. Could you please help me? – Shahrukh S. Nov 15 '18 at 13:49
  • Thanks @José Pereda. It really helped me. – Shahrukh S. Nov 15 '18 at 13:50
  • @ShahrukhS. Did you follow the guide provided by José Pereda? – Gnas Nov 15 '18 at 13:55

4 Answers4

10

I faced the same issue on debian after upgrading from stretch to buster, but now, everything is fine:

java --version

openjdk 11.0.4 2019-07-16

To run a java-fx application using terminal, follow these steps:

  1. Install openjfx (if it is not already installed):
    sudo apt install openjfx

  2. List the javafx library location:
    dpkg-query -L openjfx
    The output should be like this:

. /usr /usr/share /usr/share/doc /usr/share/doc/openjfx /usr/share/doc/openjfx/TODO.Debian /usr/share/doc/openjfx/changelog.Debian.gz /usr/share/doc/openjfx/copyright /usr/share/openjfx /usr/share/openjfx/lib /usr/share/openjfx/lib/javafx.properties /usr/share/openjfx/lib/javafx.base.jar /usr/share/openjfx/lib/javafx.controls.jar /usr/share/openjfx/lib/javafx.fxml.jar /usr/share/openjfx/lib/javafx.graphics.jar /usr/share/openjfx/lib/javafx.media.jar /usr/share/openjfx/lib/javafx.swing.jar /usr/share/openjfx/lib/javafx.web.jar

  1. Run the jar application by including the javafx path and modules:
    java --module-path $PATH_TO_OPENJFX-LIB --add-modules module_1,module_2,module_3,...,module_n -jar $PATH_TO_JAR_FILE

Example:

java --module-path /usr/share/openjfx/lib --add-modules=javafx.controls,javafx.fxml,javafx.base,javafx.media,javafx.web,javafx.swing -jar '/home/lotfi/Documents/MyAppfolder/my_application.jar'
Giulio Caccin
  • 2,962
  • 6
  • 36
  • 57
Lotfi Hocini
  • 109
  • 1
  • 4
2

jdk 11 does not have javafx support. Oracle removed it. But you can add javafx to your project using Maven.

1

I had a similar error when executing a java application, I could not find some packages already installed, I located the path where the application was reading and there I added a symbolic link to the javafx library that I had installed.

Example:

user@server:/usr/lib/jvm/java-8-openjdk-amd64/jre/lib$ sudo ln -s /usr/share/openjfx/lib/javafx.properties
clau
  • 11
  • 2
0

In Eclipse : run configurations -> arguments -> VM arguments :

--module-path ${project_classpath:REPLACE_ME_WITH_YOUR_PROJECT_NAME} 

--add-modules javafx.controls,javafx.fxml
David Buck
  • 3,752
  • 35
  • 31
  • 35