简体   繁体   中英

Java Main class cannot be found or loaded error

I am new to JavaFX and currently I am trying to build a simple Stage with a few components inside. I have imported the jfxrt.jar file and I am able to use the libraries successfully in my class App.java . Howerver, when I run my code I get the following error message on the console:

Error: Could not find or load main class App
Reason: java.lang.NoClassDefFoundError: javafx/application/Application

This is my code:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


public class App extends Application {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        launch(args);
    }

    @Override
    public void start(Stage firstStage) throws Exception {
       firstStage.setTitle("Image Processing App - Andromachi Rozaki");     
       Button button = new Button();
       button.setText("Open Image");

       StackPane layout = new StackPane();
       layout.getChildren().add(button);

       Scene scene = new Scene(layout, 300, 250);
       firstStage.setScene(scene);
       firstStage.show();
    }

}

Does anyone understand why this happens and how I could go about solving it? Thanks in advance

EDIT

I am running the App.java file in the link here

Not sure what IDE you are using but you may try to put this in your VM options

--module-path PATH_TO_JAR --add-modules javafx.controls,javafx.fxml

Thing is JavaFX is not bundled in current versions of Oracle JDK . I think you're using Java 11 , and that means JavaFX is not included .

I understand you already downloaded JavaFX library, did you included it as library in your IDE?

From screenshot I don't understand which IDE you're using, but most times you can add the jar file into a folder of your project, right-click it and find an option like add to project as library . That might solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM