Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
    }
}
like image 337
Shahrukh S. Avatar asked Nov 15 '18 12:11

Shahrukh S.


People also ask

Could not find or load main class cmd error?

The error 'Could not find or load main class' occurs when using a java command in the command prompt to launch a Java program by specifying the class name in the terminal. The reason why this happens is mostly due to the user's programming mistake while declaring the class.


3 Answers

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'
like image 70
Lotfi Hocini Avatar answered Oct 19 '22 06:10

Lotfi Hocini


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

like image 2
Alexandr Tkach Avatar answered Oct 19 '22 07:10

Alexandr Tkach


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
like image 1
clau Avatar answered Oct 19 '22 05:10

clau