简体   繁体   English

如何使用Java 9中的类加载器访问资源

[英]How to access resource using class loader in Java 9

I have a gradle project in eclipse. 我在日食中有一个gradle项目。 Here is the structure of my project 这是我的项目的结构

项目结构

I have css resource styleclass.css in scr/main/resources/css . 我在scr/main/resources/css有css资源styleclass.css First I tried to access it using 首先,我尝试使用它来访问它

scene.getStylesheets().add("css/styleclass.css"); 

But I was getting warning resource not found. 但是我resource not found.警告resource not found. I also tried it by removing module-info.java file. 我也通过删除module-info.java文件来尝试它。 But result is same. 但结果是一样的。

Then I tried it using 然后我尝试使用它

String urlString = ComboBoxStyling.class.getClassLoader().getResource("css/styleclass.css").toExternalForm();

Problem is, this line works if I remove moduele-info.java and style sheet applied. 问题是,如果我删除了moduele-info.java并应用了样式表,则该行有效。 But with module-info.java I am getting null pointer exception. 但是对于module-info.java我得到空指针异常。

I don't know much but atleast I know that class loader have changes in Java 9. So how can I do the same in Java 9. My module-info.java file contains the following 我不太了解,但至少我知道类加载器在Java 9中有变化。所以我怎么能在Java 9中做同样的事情。我的module-info.java文件包含以下内容

module pk.training.basit {
    exports pk.training.basit;
    requires transitive javafx.controls;
}

Thanks & Regards 感谢和问候

Basit Mahmood Ahmed Basit Mahmood Ahmed

You should put your css in the same package as the class which uses it, then use Class.getResource to access it, with the relative Path. 你应该将你的css放在与使用它的类相同的包中,然后使用Class.getResource来访问它,使用相对的Path。

src/main/java:
  pk.training.basit
    ComboBoxStyling.java

src/main/resources:
  pk.training.basit
    ComboBoxStyling.css

Then in the source: 然后在源中:

scene.getStylesheets().add(
  ComboBoxStyling.class.getResource("ComboBoxStyling.css").toExternalForm());

Personnally I think it's even a better practice to put the css and the class in the same directory, then configure gradle so It can take the resources in the src/main directory. 在个人情况下,我认为将css和类放在同一目录中更好的做法,然后配置gradle,这样就可以获取src / main目录中的资源。 It permits to have an architecture analog to other GUI products (Angular) which places all the resources of a screen including the source in the same directory, the FXML file for example if you had one. 它允许具有与其他GUI产品(Angular)类似的体系结构,其将屏幕的所有资源(包括源)放在同一目录中,例如FXML文件(如果有的话)。

From ClassLoader.getResource JavaDoc: 来自ClassLoader.getResource JavaDoc:

Resources in named modules are subject to the encapsulation rules specified by Module.getResourceAsStream. 命名模块中的资源受Module.getResourceAsStream指定的封装规则的约束。 Additionally, and except for the special case where the resource has a name ending with ".class", this method will only find resources in packages of named modules when the package is opened unconditionally (even if the caller of this method is in the same module as the resource). 此外,除了资源的名称以“.class”结尾的特殊情况之外,此方法只会在无条件打开包时找到命名模块包中的资源(即使此方法的调用方是相同的)模块作为资源)。

So, to fix your issue, you should make the package css open: 因此,要解决您的问题,您应该打开包css

module pk.training.basit {
    exports pk.training.basit;
    requires transitive javafx.controls;
    opens css;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM