5

To the best of my knowledge, Class.getResourceAsStream() gets an InputStream directed at a file within the .jar file? If I'm wrong about this, please correct me.

Is there any way to temporarily 'load' an external file into the .jar, so that the getResourceAsStream() method can retrieve it?

For example loading C:/folder/file.txt into the .jar so that MyClass.class .getResourceAsStream("file.txt") would return a working InputStream to the file, or at least a copy of it that has been 'loaded' temporarily into the .jar.

I'm sorry I've worded this so badly, I hope you can understand what I'm trying to do, wasn't quite sure how to explain it simply.

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
  • 3
    I don't believe you can do this. Perhaps some explanation of what you're ultimately trying to achieve would be useful ? – Brian Agnew Feb 20 '13 at 10:44
  • 3
    Why do you want to do this? If you know where the file is located, just load it directly. – Mark Rotteveel Feb 20 '13 at 10:44
  • 1
    Or put the directory where the file is located in the classpath. – JB Nizet Feb 20 '13 at 10:45
  • 1
    What I'm ultimately trying to achieve is loading a .png image so that it can be accessed through this method. The reason it must be through .getResourceAsStream() is because I'm making an Applet Modification, and can't change how the image is accessed. –  Feb 20 '13 at 10:45

1 Answers1

4

Nope, the getResourceAsStream() method gets a file from the classpath. That includes anything that is specified like this on the command line (windows version, linux uses ':' as delimiter):

java -cp "path;path2;path3;path4/some.jar" your.main.Clazz
John Smith
  • 2,282
  • 1
  • 14
  • 22
  • also in a servlet container, the classpath handling might be a little difficult (since tomcat determines what is on the classpath and what is not, and Class.getClassLoader() might return null in some situations. – John Smith Feb 20 '13 at 10:48