1

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?

Dimitris Sfounis
  • 2,400
  • 4
  • 31
  • 46
  • Do not attempt to use resources on the class path as files! This obviously won't work. There is no `File` within jars at runtime. Use resources. – fge Mar 21 '16 at 13:06
  • @DimitrisSfounis Can you try with either `getClass().getResource("/css/default_skin.css").toExternalForm();` or `getClass().getClassLoader().getResource("css/default_skin.css").toExternalForm();` – Madhusudana Reddy Sunnapu Mar 21 '16 at 13:06
  • @MadhusudanaReddySunnapu Tried both, no dice. – Dimitris Sfounis Mar 21 '16 at 13:52

3 Answers3

1

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();
fabian
  • 80,457
  • 12
  • 86
  • 114
  • Does not work, so that means I probably haven't added the css file to the classpath properly. – Dimitris Sfounis Mar 21 '16 at 14:47
  • @DimitrisSfounis: Just check the `build` folder or the jar under `dist` using the `Files` view. This should allow you to check whether the files are present or not. – fabian Mar 21 '16 at 14:52
  • css file is there under `build/classes/css/default_skin.css`. – Dimitris Sfounis Mar 21 '16 at 14:53
  • @DimitrisSfounis Have you tried clean & build ? Do you run it from the jar or from the `build` folder? You don't have excluded css files from being packaged (Project properties -> Build -> packaging) by chance? – fabian Mar 21 '16 at 14:58
  • I run it through Netbeans, which probably means it runs the `jar` from temporary `dist` folders. Packaging is not set to ignore .css, no. Thanks for all the help so far, in any case, friend. I appreciate it very much. – Dimitris Sfounis Mar 21 '16 at 15:10
0

You need to add css folder in classpath if you want retrieve using getClass().getResource("css/default_skin.css").toExternalForm();

Vitthal Kavitake
  • 879
  • 5
  • 18
  • How do I do that? Why did I not have to do this when loading graphics or the properties file in my application? – Dimitris Sfounis Mar 21 '16 at 12:54
  • If you are running this from eclipse, you need to go to `properties --> Java build path` and add source folder. If you are packaging your application then you need to specify css folder to be go into jar file according to packaging tool specification – Vitthal Kavitake Mar 21 '16 at 12:57
  • I forgot to mention that the `css/default_skin.css` folder is under the `src` package. The same way `graphics` are, which also get loaded fine by `getClass().getResourceAsStream()` – Dimitris Sfounis Mar 21 '16 at 13:02
0

Check if you are not using binary encode of CSS files (see Project Properties>Packaging). If you are using, the mentioned file extension should be ".bss".