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

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

问题描述

我的项目在eclipse中正常运行,但是当我创建一个这个项目的jar文件并尝试通过cmd运行它时显示Location is not set错误。

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.

FYI:我对文件夹结构进行了一些更改,并且能够成功运行。但是这个结构是可怕的,因为我将所有的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。 shortName
和扩展名都必须是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中工作,但是在...中实现 getResource(...) jar类加载器不按照你所希望的方式实现。

(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"));

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

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