简体   繁体   中英

Unable to run the javafx project using command line

I recently had created the javafx project in eclipse, I successfully ran in eclipse and it just works fine. I wanted to run my project using the command line interface.

I am able to compile successfully but not able to run and it keeps on giving "Error: could not find or load main class Main"

Compile command (Works fine): 

javac -cp "C:\Program Files (x86)\Oracle\JavaFX 2.2 Runtime\lib\jfxrt.jar" Main.java

Main.class is created after the above command.

Run Command (doesn't work):
java -cp "C:\Program Files (x86)\Oracle\JavaFX 2.2 Runtime\lib\jfxrt.jar;." Main

I would like to know what am I missing here in order to successfully run it.

Adding Main.java

package application;

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;


public class Main extends Application {
    private Stage primaryStage;
    private BorderPane root;
    @Override
    public void start(Stage primaryStage) {
        try {
            this.primaryStage = primaryStage;
            primaryStage.setTitle("Mojo");
            initRootLayout();
            showMapLayout();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    private void showMapLayout() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("/application/viewControllers/mapView.fxml"));
            AnchorPane mapPane = (AnchorPane)loader.load();
            root.setCenter(mapPane);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void initRootLayout() {
        try{
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("/application/viewControllers/RootLayout.fxml"));

            root = (BorderPane)loader.load();
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.show();
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

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

Your class is declared to be in the package application . So, to compile it so that you get the correct folder structure you should compile with the -d option:

javac -cp "C:\Program Files (x86)\Oracle\JavaFX 2.2 Runtime\lib\jfxrt.jar" -d . Main.java

That should create a directory called application , and Main.class will be in there.

Then execute (from the same folder, not from the application folder) with

java -cp "C:\Program Files (x86)\Oracle\JavaFX 2.2 Runtime\lib\jfxrt.jar";. application.Main

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