resource : Java Glossary

resource
A data file embedded inside the jar file.
Use Old Netscape
Recipe Links
Cramfull

Use

You can access the resource as a URL (Uniform Resource Locator) :
or as an InputStream:
where InWords.properties is the name of the file inside the jar you want to read.

Class.getResource is similar, giving you the URL of the resource instead of an InputStream to read it. Note that getResource is an instance method. Class.getResource makes these changes to the resource name: if the resource name starts with "/", it is unchanged; otherwise, the package name is prepended to the resource name after converting "." to "/". This allows you to use either dots or slashes to separate the components of the name. So normally your resource member name includes the package name, but you don’t specify the package name to getResource. Alternatively, but not recommended, you can specify the fully qualified name of the resource by using a lead / on the name you feed to getResource. Never use \ in resource names. So you can access by either: /com/mindprod/entities/entitytable.ser or the short form entitytable.ser. The short form only works from classes in the com.mindprod.entities package.

Recipe

Here my recipe for accessing resources:
  1. Using jar.exe, pack the resource in the jar under the package name and resource name. For example, a resource for use by com.mindprod.entities. Entities would be stored in the jar under the name com/mindprod/entities/entitytable.ser. Note that the class name does not appear, just the package and resource name. See jar.exe for how. Use WinZip or similar ZIP utility to verify the package name and resource name are correct inside the jar, including case.
  2. // accessing a resource with getResource to give you its URL
    
    URL url = Entities.class.getResource( "entitytable.ser" );
    System.out.println( url );
    
    // Note that getResource is an instance method of Class,
    // so You CANNOT say:
    URL url = Class.getResource( "entitytable.ser" );
    Note the lack of dots, slashes, package name or class name in the resource.

Tip

Cramfull

The problem with resources is you must manually remember to include them. You might not find out they are missing from the jar until months later when someone goes to use an obscure resource. GenJar is incapable of detecting missing resources or automatically including them. Cramfull is a tool to convert resources into Java source code. The advantage of Cramfull is GenJar can detect resources coded as class files and make sure they are included.

Old Netscape

class.getResource does not work in some versions of Netscape. You must use class.getResourceAsStream. Further, for some Satanic reason, the folk at Netscape decided that resources must have one of a magic list of extensions. *.com, *.exe, *.dat are not among them. Happily *.jpg, *.gif and *.ser are. Images are a very common sort of resource.

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/resource.html J:\mindprod\jgloss\resource.html
logo
Please email your , letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, please quote it and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. Threatening to kill me or spouting obscenities has yet to persuade me to change my mind.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.210]
You are visitor number 23,942.