[英]a Java application works fine on Eclipse but it doesn't work properly after being deployed as a runnable jar
[英]Runnable Jar file doesn't work but the source code works fine. Maven Project JavaFX
我目前正在学习JavaFX,并尝试构建小型GUI。 我想我做得很好。 唯一的问题是,当我从Eclipse运行程序时,它运行得很顺利,但是当我将其导出为Runnable Jar File并尝试在外部运行时,它不起作用。
这是一个maven项目,下面是我程序的主要方法:
public class Main extends Application {
private Stage stage;
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("/view/main.fxml"));
this.stage = primaryStage;
primaryStage.setTitle("SLATE");
primaryStage.setScene(new Scene(root, 800, 700));
primaryStage.show();
}
public Stage getStage() {
return stage;
}
public static void main(String[] args) {
launch(args);
}
}
我试图通过从终端运行JAR来调试它,以下是错误:
Exception in Application start method
java.lang.reflect.InvocationTargetException
.....
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
我认为这与我猜想的资源路径有关,但是我无法解决任何问题。 奇怪的是,它在Eclipse中有效,但JAR文件没有相同的代码。
非常感谢您的帮助。 注意: 项目视图
您的视图路径似乎错误(因为它使用当前类的包名称),请尝试以下方法:
FXMLLoader loader = new FXMLLoader();
Parent rootNode = (Parent) loader.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("view/main.fxml"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.