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);
}
}
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.
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
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.
After looking inside this temporary folder while running the program, though, I can find no css folder nor css file.
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?
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
).
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
?