JAVAFX:位置未设置错误 [英] JAVAFX: Location is not set error

查看:37
本文介绍了JAVAFX:位置未设置错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目在 eclipse 中运行正常,但是当我创建这个项目的 jar 文件并尝试通过 cmd 运行它时,它显示位置未设置"错误.

My project is running properly in eclipse but when I am creating a jar file of this project and trying to run it through cmd it is showing "Location is not set" error.

我的项目结构是:

方法是(在eclipse中运行):

The Method is (Running in eclipse):

@FXML
private void RegularCustomer(ActionEvent event) throws Exception{
    Stage stage = (Stage) dailySales.getScene().getWindow();
    Scene scene = dailySales.getScene();
    FXMLLoader loader = new FXMLLoader(getClass().getResource("../customer/CustomerHome.fxml"));
    System.out.println(loader.getLocation());
    scene.setRoot(loader.load());
    stage.setScene(scene);
    stage.show();
}

这段代码有什么问题?

有一些相对的问题,但它们是不同的.他们的代码没有在 IDE 中运行,但我的代码在 IDE 中运行.

There are some relative questions but they are different from it. Their code didn't run in IDE but my code run in IDE.

仅供参考:我对文件夹结构进行了一些更改,并且能够成功运行.但这种结构很糟糕,因为我将所有 FXML 文件和控制器放在同一个包中.

FYI: I made some changes in folder structure and was able to run successfully. But that structure was horrible because I put all my FXML files and controllers in same package.

推荐答案

当您使用 getClass().getResource(...) 时,您正在加载资源,而不是指定文件路径.在类加载器从文件系统加载类的情况下,这些基本上等同于同一件事,并且它确实可以工作(尽管即使那样也没有技术原因必须这样做).当类加载器通过其他机制(并且可能在所有情况下)加载类时,注意 Java 资源规范.

When you use getClass().getResource(...) you are loading a resource, not specifying a path to a file. In the case where the class loader loads classes from the file system, these essentially equate to the same thing, and it does actually work (though even then there's no technical reason it has to). When the class loader is loading classes by other mechanisms (and probably in all cases anyway), then it's important to pay attention to the Java specifications for a resource.

特别注意:

资源、名称和上下文

一个资源由一个由一系列的字符串组成的字符串标识子字符串,由斜杠 (/) 分隔,后跟资源名称.每个子字符串都必须是有效的 Java 标识符. 资源名称的格式为 shortName 或 shortName.extension.两者都简称和扩展名必须是 Java 标识符.

A resource is identified by a string consisting of a sequence of substrings, delimited by slashes (/), followed by a resource name. Each substring must be a valid Java identifier. The resource name is of the form shortName or shortName.extension. Both shortName and extension must be Java identifiers.

(我的重点.)因为 .. 不是有效的 Java 标识符,所以不能保证该资源是可解析的.碰巧文件系统类加载器以您期望的方式解决了这个问题,这就是它在您的 IDE 中工作的原因,但是 jar 类加载器中的 getResource(...) 的实现没有实现这是您希望的方式.

(My emphasis.) Since .. is not a valid Java identifier, there's no guarantee of this resource being resolvable. It happens that the file system class loader resolves this in the way you expect, which is why it works in your IDE, but the implementation of getResource(...) in the jar class loader does not implement this in the way you are hoping.

试试

FXMLLoader loader = new FXMLLoader(getClass().getResource("/sm/customer/CustomerHome.fxml"));

<小时>

使用控制器位置加载 FXML:

由于您已经组织了代码,因此每个 FXML 都与其对应的控制器文件位于同一个包中(我认为这是一种明智的做法),您还可以在加载 FXML 时利用它:只需加载 FXML相对于其控制器":


Using controller locations to load FXML:

Since you have organized your code so that each FXML is in the same package as its corresponding controller file (which I think is a sensible way to do things), you could also leverage this in loading the FXML: just load the FXML "relative to its controller":

FXMLLoader loader = new FXMLLoader(CustomerHomeCtrl.class.getResource("CustomerHome.fxml"));

这在此设置中似乎相当自然,编译器将检查您在导入类时CustomerHomeCtrl 的包名称是否正确.它还使重构变得容易:例如,假设您想将 sm.admin 拆分为多个子包.在 Eclipse 中,您将创建子包,将 FXML 和控制器拖放到适当的子包中,并且导入语句将自动更新:不需要进一步的更改.如果在 getResource(...) 中指定了路径,则所有这些都必须手动更改.

This seems fairly natural in this setup, and the compiler will check that you have the package name for CustomerHomeCtrl correct at the point where you import the class. It also makes it easy to refactor: for example suppose you wanted to split sm.admin into multiple subpackages. In Eclipse you would create the subpackages, drag and drop the FXML and controllers to the appropriate subpackages, and the import statements would automatically be updated: there would be no further changes needed. In the case where the path is specified in the getResource(...), all those would have to be changed by hand.

这篇关于JAVAFX:位置未设置错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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