getResourceAsStream : Java Glossary
home G words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish 2007-10-23 by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
getResourceAsStream
java.lang.Class.getResourceAsStream lets you read files in jars, or in the directory from which the Applet class files were loaded.
Use Old Netscape
Troubleshooting Links

Use

You use it like this:
The resource in the jar file must be qualified with same package name as the class you call getResourceAsStream from. Alternatively, you can use an absolute name beginning with a / where dots get mapped to /s. If you don’t have a lead /, you have a relative name, and the name of the package will be prepended. If you use a /, you must include the name of the package yourself, or whatever name the resource is filed under in the jar.

For example you could specify "/com/mindprod/mypackage/mystuff.ser" or "/com.mindprod.mypackage.mystuff.ser" or simply "mystuff.ser". Don’t use Windows style filenames with \. These are not filenames, but Java resources that live along with the class files either in jars or sometimes freestanding on disk, or on the server.

In theory, getResourceAsStream will look in the local classpath, in the jar and in the directory where the class file was loaded from, though I have found in practice it is usually not that thorough. Beware, you might be getting a different copy of a file than the one in the jar you expected. In theory this should not be necessary, but test your jarred Applets with an empty classpath.

I have seen a variety of bug reports surrounding getResource and getResourceAsStream. They sometimes can’t see inside jars, see files on the server or see local files on the classpath. This is odd, since supposedly the usual ClassLoader mechanism does the looking, it has much less trouble finding class files.

Troubleshooting

If getResourceAsStream does not work, check the following:
  1. Resources are much more tractable in jars rather than free floating.
  2. Is the APPLET CODEBASE tag set? Remove it. It seems to interfere.
  3. Is the folder qualification of the class calling getResourceAsStream in the jar identical, including case, to the qualification of the resource?
  4. Don’t use the same name for a resource in different jars and expect to always get the version in the current jar.
  5. Make sure your resources have unique fully qualified names across the entire website.
  6. getResourceAsStream is case sensitive. Make sure the case of your filename exactly matches the name of the resource in the jar to avoid trouble. Make sure the original filename, the name on the jar.exe command line and the name in the jar are all correct. You can be fooled because getResourceAsStream can sometimes work with the wrong case when the file is not in a jar.

Old Netscape

Unfortunately, getResourceAsStream is broken in Netscape 4.79, as is getResource. Oddly, I could swear I had it working in one of the slightly earlier versions of Netscape, though.

Netscape 4.79 getResourceAsStream has a bug. It does not properly prepend the package name on the resource. It uses it as is.

For some Satanic reason, the folk at Netscape decided that resources must have one of a magic list of extensions. .com, .exe, .dll, .dat are not among them. Happily .jpg, .gif, .ram and .ser are. To read other extensions, you must first invoke SecurityManager.enablePrivilege( "UniversalPropertyRead" ).

class
ClassLoader
Images: accessing Image resources
Netscape
resource

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.17] The information on this page is for non-military use only.
You are visitor number 84,828. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/getresourceasstream.html J:\mindprod\jgloss\getresourceasstream.html