简体   繁体   English

无法使用命令行运行javafx项目

[英]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. 我最近在eclipse中创建了javafx项目,我在eclipse中成功运行,并且运行良好。 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" 我能够成功编译,但无法运行,并且不断显示“错误:找不到或加载主类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 添加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 . 您的类被声明为在包application So, to compile it so that you get the correct folder structure you should compile with the -d option: 因此,要对其进行编译,以便获得正确的文件夹结构,应使用-d选项进行编译:

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. 那应该创建一个名为application的目录, Main.class将在其中。

Then execute (from the same folder, not from the application folder) with 然后执行(从同一文件夹,而不是从application文件夹)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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