Reading a file from a JAR that does not work with windows

I am trying to read from a file that is packed inside a JAR, along with a class that reads it. For this, I use the following:

getClass().getClassLoader().getResourceAsStream(file)

This works fine when I create and run a JAR file on OSX, but if I create and run a JAR file on Windows, the above line returns null.

Am I missing something? If I create a JAR on OSX and run it on Windows, it works fine. The problem only occurs when creating a JAR on windows.

EDIT: It is worth noting that there is no folder hierarchy in the JAR file. Everything is stored at the same level, so the class that reads the file and the file itself are in the same directory. In addition, this is how I create the JAR file on both OSX and Windows:

jar -cmf manifest.mf run.jar *.class file1 file2

EDIT 2: The file I'm trying to download is a java.properties file. I suppose this is not what causes the problem?

+5
source share
3 answers

Skip the part of the class loader. Just getClass (). GetResource ....

0
source

Try using getClass (). getResourceAsStream ("/ file1").

0
source

When using file delimiters, do not chew to encode them! Use java.io.File.separator instead: http://docs.oracle.com/javase/7/docs/api/java/io/File.html#separator

-1
source

All Articles