为什么相对路径在JAR文件的JAVA中不起作用? [英] Why relative path doesn't work in JAVA in JAR-file?

查看:265
本文介绍了为什么相对路径在JAR文件的JAVA中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的JavaFX项目.它来自:

I have a simple JavaFX project. It consists from:

  • src/app/Main.java
  • src/app/controller/MainController.java
  • src/app/controller/SecondaryWindowController.java
  • src/app/view/Main.fxml
  • src/app/view/SecondaryWindow.fxml

控制器 MainController.java 具有方法(按下按钮的处理程序):

The controller MainController.java has method (handler of press button):

public void openWindow(ActionEvent event) {
    Pane root = null;
    try {
        System.out.println("URL: " + getClass().getResource("../view/SecondaryWindow.fxml"));
        FXMLLoader loader = new FXMLLoader(getClass().getResource("../view/SecondaryWindow.fxml") );
        root = loader.load();
    }
    catch(Exception e) {
        e.getMessage();
        e.printStackTrace();
    }
    if (root != null) {
        Scene scene = new Scene(root,400,100);
        Stage secStage = new Stage();
        secStage.setTitle("Secondary Window");
        secStage.setScene(scene);
        secStage.show();
    }
}

如果我从编译文件运行项目,则

代码 getClass().getResource("../view/SecondaryWindow.fxml")返回正确的URL.但是如果我从JAR文件运行项目,它将返回 null .

Code getClass().getResource("../view/SecondaryWindow.fxml") return right URL if I run project from compile files. But it return null if I run project from JAR-file.

我使用以下命令编译文件:

I compile files using:

javac -d bin src\app\Main.java
javac -d bin src\app\controller\*.java
XCOPY src\app\view\*.fxml bin\app\view /s /e /d /y

并使用以下命令运行编译文件:

and run compile files using:

java -classpath bin app.Main

目录 bin 具有类似的文件结构:

Catalog bin has similar structure of files:

  • bin/app/Main.class
  • bin/app/controller/MainController.class
  • bin/app/controller/SecondaryWindowController.class
  • bin/app/view/Main.fxml
  • bin/app/view/SecondaryWindow.fxml

然后我使用以下命令创建JAR文件:

And I create JAR-file using:

jar cvfm SimpleFXMLApp.jar manifest.mf -C bin app

Manifest.mf

Manifest.mf

Manifest-Version: 1.0
Created-By: Denys Selivanov
Main-Class: app.Main

为什么相对路径在 JAR文件 getClass().getResource("../view/SecondaryWindow.fxml")中不起作用返回? 该如何修复?

Why relative path doesn't work in JAR-file and getClass().getResource("../view/SecondaryWindow.fxml") return null ? How repair that?

要解决的问题之一是将控制器和视图文件保存在一个目录中,并使用相对路径"SecondaryWindows.fxml",但我想保留项目的结构文件.

One of the problem's solves is keep controllers and views files in one catalog and use relative path "SecondaryWindows.fxml", but I want keep my structure files of project.

您可以在此处下载我的项目.此链接将访问30天.

And you can download my project here. This link will be access 30 days.

推荐答案

我看到闭幕投票说这是

I see the vote to close that says it's a duplicate of this Q&A, but I don't think it explains the specifics of .., so here you go:

..在文件系统上的资源上工作,因为..是文件系统上的实际条目,指向父目录.但这在jar中不存在,因此当您的类&资源包装在一个罐子里.

.. works on resources on the filesystem, because .. is an actual entry on the filesystem, that points to parent directory. But that does not exist within jars, so it won't work to point to "parent package" when your classes & resources are packaged in a jar.

解决方案:如果您可以接受从控制器类到Main类的依赖关系,则可以改为:

Solution: if you can accept the dependency from your controller class to Main class, you can do this instead:

Main.class.getResource("view/SecondaryWindow.fxml")

无论如何,您都需要引用一个在程序包层次结构中足够高的类,或者使用来自根程序包的绝对路径(显然是getClass().getClassLoader().getResource("app/view/SecondaryWindow.fxml")).

In any case you'll need to either reference a class that is high enough in the packages hierarchy, or use an absolute path from root package (getClass().getClassLoader().getResource("app/view/SecondaryWindow.fxml") in your case apparently).

这篇关于为什么相对路径在JAR文件的JAVA中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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