通过getClass()加载文件.getResource()失败,File()工作 [英] File loading by getClass().getResource() fails, File() works
问题描述
我正在尝试通过以下代码片段将css文件合并到我的JavaFX应用程序中:
I'm trying to incorporate a css file to my JavaFX appllication, by the following snippet:
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);
}
}
运行时产生的结果:
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.
public void readPropertiesFile() {
Properties props = new Properties();
InputStream is;
try {
File f = new File("properties");
is = new FileInputStream(f);
}
/* etcetera */
}
这是有效的喜欢魅力。 属性
文件位于项目目录的根目录, C:\ ~my_projects_folder~ \ RoobotikosAnomologitos
This works like a charm. The properties
file is located at the root of the project directory, C:\~my_projects_folder~\RobotikosAnomologitos
在寻找解决方案后,我看到 getClass()。getResource()
尝试在类路径中查找资源。我试图在每一个版印刷类路径中,我得到:
RobotikosAnomologitos\dist\run125323585\RobotikosAnomologitos.jar
这足以逻辑
After searching around for a solution, I saw that getClass().getResource()
attempts to find a resource in the classpath. I tried printing the classpath at every run, and I get:
RobotikosAnomologitos\dist\run125323585\RobotikosAnomologitos.jar
which is logical enough.
在运行程序的同时查看这个临时文件夹后,我找不到css文件夹或css文件。
After looking inside this temporary folder while running the program, though, I can find no css folder nor css file.
但该文件确实位于我的工作项目目录中,位于 RobotikosAnomologitos \css \default_skin.css
下。由于某种原因,它不会在运行时在类路径中生成它,导致 getResource()
在查找时返回null。
But the file is indeed located in my working project directory, under 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.
关于如何加入它的任何想法?
Any ideas on how to include it?
编辑:我忘记了提到我还在 src
包下放置了 css / default_skin.css
,并显示在Netbeans的包中树( src / css / default_skin.css
)。
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 / graphics /
下的 getClass()。getResourceAsStream()
。对于无法找到css文件的原因,这也让我感到困惑。也许它不会在 jar
中编译?
In the same way, I have some graphics that are located under src/graphics/
which get loaded fine by getClass().getResourceAsStream()
. Which also bafflesss me as to why the css file can't be found. Maybe it doesn't get compiled in the jar
?
推荐答案
如果你为一个类调用 getResource()
并且不预置 /
,该路径被认为是相对于该类的包。
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.
如果你已经将资源正确地添加到类路径中,这应该可行:
If you've properly added the resources to the classpath, this should work:
skinCSS = getClass().getResource("/css/default_skin.css").toExternalForm();
这篇关于通过getClass()加载文件.getResource()失败,File()工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!