Search code examples
javawindowsunixpathseparator

Multiplatform getResourceAsStream


What file separators should I use in order to specify a path to resource? I run the packed program java -jar program.jar.

Currently I use the following loading

 inputStream = getClass.getResourceAsStream(
                   File.separator + "resources" + File.separator + "my.class");

On linux, it's correctly evaluated to /resources/my.class and the class is loaded. However, on Windows a NullPointerException is thrown. What is the correct use? I desperately need my program to be multiplatform.

I'm a bit confused since the documentation informs that the path may start with / which isn't a Windows separator.


Solution

  • A resource path should always use '/' as it is not conceptually looking for a file path but a resource path within a class path entry.