无法使用命令行运行javafx项目 [英] Unable to run the javafx project using command line

查看:609
本文介绍了无法使用命令行运行javafx项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在eclipse中创建了javafx项目,我成功运行了eclipse,它运行正常。
我想使用命令行界面运行我的项目。

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.

我能够成功编译但无法运行并且它继续给出错误:无法找到或加载主类Main

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.

添加Main.java

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


推荐答案

您的class被声明在包应用程序中。因此,要编译它以便获得正确的文件夹结构,您应该使用 -d 选项进行编译:

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

这应该创建一个名为 application Main.class 将在那里。

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

然后执行(来自同一文件夹,而不是来自<$ p $ c>应用程序文件夹)

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

这篇关于无法使用命令行运行javafx项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆