Patrick Rath
Patrick Rath

Reputation: 28

InputStream from jar-File returns always null

i know this question has been asked several times, but i think my problem differs a bit from the others:

String resourcePath = "/Path/To/Resource.jar";

File newFile = new File(resourcePath);
InputStream in1 = this.getClass().getResourceAsStream(resourcePath);
InputStream in2 = this.getClass().getClassLoader().getResourceAsStream(resourcePath);

The File-Object newFile is completely fine (the .jar file has been found and you can get its meta-data like newFile.length() etc)

On the other hand the InputStream always return null. I know the javadoc says that the getResourceAsStream() is null if there is no resource found with this name, but the File is there! (obviously, because it's in the File-Object)

Anyone know why this happens and how i can fix it so that i can get the .jar File in the InputStream?

Upvotes: 0

Views: 1106

Answers (1)

mthmulders
mthmulders

Reputation: 9705

The getResourceAsStream() method doesn't load a file from the file system; it loads a resource from the classpath. You can use it to load, for example, a property file that's packaged inside your JAR. You cannot use it to load a file from the file system.

So, if your file resides on the file system, rather than in your JAR file, better use the FileInputStream class.

Upvotes: 1

Related Questions