简体   繁体   English

getClass().getResource() 加载文件失败,File() 有效

[英]File loading by getClass().getResource() fails, File() works

I'm trying to incorporate a css file to my JavaFX appllication, by the following snippet:我正在尝试通过以下代码段将 css 文件合并到我的 JavaFX 应用程序中:

public void loadExternalCSS() {
      System.out.println("CLASSPATH: "+System.getProperty("java.class.path"));
      try{
        skinCSS = getClass().getResource("css/default_skin.css").toExternalForm();
      }
      catch(Exception e){
          System.err.println("Exception: " + e);
          e.printStackTrace(System.err);
      }
}

Which yields, at runtime:在运行时产生:

java.lang.NullPointerException
    at 
robotikosanomologitos.RobotikosAnomologitos.loadExternalCSS(RobotikosAnomologitos.java:529)
    at robotikosanomologitos.RobotikosAnomologitos.start(RobotikosAnomologitos.java:491)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

The weird thing is that a few lines above this function, I have a small function that reads from a properties file.奇怪的是,在这个函数的上面几行,我有一个从properties文件中读取的小函数。

public void readPropertiesFile() {
        Properties props = new Properties();
        InputStream is;
        try {
            File f = new File("properties");
            is = new FileInputStream(f);
        }
        /* etcetera */
}

This works like a charm.这就像一个魅力。 The properties file is located at the root of the project directory, C:\\~my_projects_folder~\\RobotikosAnomologitos properties文件位于项目目录的根目录, C:\\~my_projects_folder~\\RobotikosAnomologitos

After searching around for a solution , I saw that getClass().getResource() attempts to find a resource in the classpath.在四处寻找解决方案后,我看到getClass().getResource()尝试在类路径中查找资源。 I tried printing the classpath at every run, and I get: RobotikosAnomologitos\\dist\\run125323585\\RobotikosAnomologitos.jar which is logical enough.我尝试在每次运行时打印类路径,我得到: RobotikosAnomologitos\\dist\\run125323585\\RobotikosAnomologitos.jar ,这是合乎逻辑的。

After looking inside this temporary folder while running the program, though, I can find no css folder nor css file.但是,在运行程序时查看此临时文件夹后,我找不到 css 文件夹和 css 文件。

But the file is indeed located in my working project directory, under RobotikosAnomologitos\\css\\default_skin.css .但该文件确实位于我的工作项目目录中,位于RobotikosAnomologitos\\css\\default_skin.css For some reason it doesn't make it in the classpath at runtime, causing getResource() to return null when looking for it.出于某种原因,它在运行时没有出现在类路径中,导致getResource()在查找时返回 null。

Any ideas on how to include it?关于如何包含它的任何想法?


EDIT: I forgot to mention that I have also placed css/default_skin.css under the src package, and shows up in Netbeans' package tree ( src/css/default_skin.css ).编辑:我忘了提到我还在src包下放置了css/default_skin.css ,并显示在 Netbeans 的包树( src/css/default_skin.css )中。

In the same way, I have some graphics that are located under src/graphics/ which get loaded fine by getClass().getResourceAsStream() .同样,我有一些位于src/graphics/下的src/graphics/ ,它们可以通过getClass().getResourceAsStream()加载。 Which also bafflesss me as to why the css file can't be found.这也让我感到困惑,为什么找不到 css 文件。 Maybe it doesn't get compiled in the jar ?也许它没有在jar编译?

If you call getResource() for a class and do not prepend a / , the path is considered to be relative to the package of the class.如果您为一个类调用getResource()并且不预先添加/ ,则该路径被视为相对于该类的包。

If you've properly added the resources to the classpath, this should work:如果您已将资源正确添加到类路径,则应该可以:

 skinCSS = getClass().getResource("/css/default_skin.css").toExternalForm();

如果要使用getClass().getResource("css/default_skin.css").toExternalForm();检索,则需要在classpath添加css文件夹getClass().getResource("css/default_skin.css").toExternalForm();

Check if you are not using binary encode of CSS files (see Project Properties>Packaging).检查您是否没有使用 CSS 文件的二进制编码(请参阅项目属性>打包)。 If you are using, the mentioned file extension should be ".bss".如果您正在使用,提到的文件扩展名应该是“.bss”。

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

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