user10559318
user10559318

Reputation:

Error: Could not find or load main class application.Main JAVAFX

Under Java Build Path Libraries under the properties of my project, I have my User Library called javafx12 under Modulepath.

enter image description here

This removed all the errors regarding import javafx not resolved.

When I try to run my project, I get

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

How do I get rid of this error?

I am using Java SE 12 aka JDK 12.

I am using eclipse as well.

enter image description here

    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);
    }
}

No errors in the class file.

Upvotes: 6

Views: 80089

Answers (4)

Mirehan Reda
Mirehan Reda

Reputation: 1

This work with me not to add fxml files separated by comma add each as separated option

Enter this in VM arguments box: --module-path "\path\to\javafx-sdk-12.0.1\lib" --add-modules javafx.controls --add-modules javafx.fxml

Upvotes: 0

Seldin
Seldin

Reputation: 31

This is how I solved this problem.

Step #1:

Step #2

  • Content of the "module-info.java" file should look like this:
module Example {
    requires javafx.fxml;
    requires javafx.controls;
    
    opens application;
} 

Upvotes: 2

Kawthar
Kawthar

Reputation: 81

This worked for me after getting the error:

Could not find or load main class application.Main Caused by: java.lang.ClassNotFoundException: application.Main while trying to use JavaFX in Eclipse

  1. Run then run configurations
  2. In Main, choose the correct Project and Main class names
  3. In Argument, enter this in VM arguments box: --module-path "\path\to\javafx-sdk-12.0.1\lib" --add-modules javafx.controls,javafx.fxml
  4. Apply

Upvotes: 8

user10559318
user10559318

Reputation:

Following @Ashish link openjfx.io/openjfx-docs

I did the following and it fixed the error:

  1. Add VM arguments To solve the issue, click on Run -> Run Configurations... -> Java Application, create a new launch configuration for your project named hellofx and add these VM arguments:

Linux/Mac Windows

--module-path "\path\to\javafx-sdk-12.0.1\lib" --add-modules javafx.controls,javafx.fxml Warning: Make sure the option:

Use the -XstartOnFirstThread argument when launching with SWT is not selected. VM arguments Click apply and close the dialog.

Upvotes: 11

Related Questions