2

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);
    }
}
2
  • Is your main class declared public class Main {....}
    – jewelsea
    Apr 14, 2015 at 23:03
  • Yes, public class Main extends Application {...}
    – Deepak
    Apr 14, 2015 at 23:04

1 Answer 1

3

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
2
  • That did not worked either. I was referring to this thread, so I tried the way it mentioned.
    – Deepak
    Apr 14, 2015 at 23:15
  • 1
    Yeah, sorry; missed the crucial point: your class is in a package. See update.
    – James_D
    Apr 14, 2015 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.