Problem with resources packed in a jar file

Discussion in 'Java' started by Sameer, Sep 25, 2005.

  1. Sameer

    Sameer Guest

    I want to deploy my Java GUI application. I have created a jar file using
    the command
    jar cvfm icClient.jar manifest.mf *.class *.gif
    When my application executes, the buttons in the applications which are
    created using gif files wont displayed.
    I checked that the jar file contains the gif files by extracting it.
    If i put the gif files in the same folder as the jar file, the icons gets
    displayed.
    The same is happening with the jar files exported with Eclipse 3.1.
    What may be the problem?
    Sameer, Sep 25, 2005
    #1
    1. Advertising

  2. Sameer wrote:
    ...
    > When my application executes, the buttons in the applications which are
    > created using gif files wont displayed.


    Does the call to Class.getResource() even find the images?

    > The same is happening with the jar files exported with Eclipse 3.1.
    > What may be the problem?


    It is probably your code.
    Andrew Thompson, Sep 25, 2005
    #2
    1. Advertising

  3. Sameer

    Sameer Guest

    The procedure of creating a jar file with files other than class files
    embedded in it is so simple and I followed the same.
    This is the problem with jar file because my application executes, and
    if gif files are put in the same folder then they are displayable in
    application.
    I haven't defined any resources.
    What is the significance of Class.getResource().
    Sameer, Sep 25, 2005
    #3
  4. Sameer wrote:
    > The procedure of creating a jar file with files other than class files
    > embedded in it is so simple and I followed the same.
    > This is the problem with jar file because my application executes, and
    > if gif files are put in the same folder then they are displayable in
    > application.
    > I haven't defined any resources.
    > What is the significance of Class.getResource().


    I guess* that your images are not being found.
    Class.getResource() is the basis of that (in however
    an indirect way you are calling it).

    * This is the best I can do with your vague
    description that lacks a stacktrace or code
    example.
    Andrew Thompson, Sep 25, 2005
    #4
  5. Sameer

    Roedy Green Guest

    On Sun, 25 Sep 2005 11:07:02 +0530, "Sameer"
    <> wrote or quoted :

    >The same is happening with the jar files exported with Eclipse 3.1.
    >What may be the problem?


    see http://mindprod.com/jgloss/jarfile.html

    You are not getting your package names into the member names of the
    classes and packages inside the jar.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Sep 25, 2005
    #5
  6. Sameer

    Mark Guest

    Sameer wrote:
    > I want to deploy my Java GUI application. I have created a jar file using
    > the command
    > jar cvfm icClient.jar manifest.mf *.class *.gif
    > When my application executes, the buttons in the applications which are
    > created using gif files wont displayed.
    > I checked that the jar file contains the gif files by extracting it.
    > If i put the gif files in the same folder as the jar file, the icons gets
    > displayed.
    > The same is happening with the jar files exported with Eclipse 3.1.
    > What may be the problem?
    >
    >
    >

    I had a similar problem. With all my class, gif, and other files in
    directories on my computer, I could use the following to get an image to
    use on a button:

    private ImageIcon xGif = new ImageIcon("x.gif");

    This would no longer work when I put everything into a jar file - the
    gif was not found. I changed the code to the following, and everything
    worked once again:

    private ImageIcon xGif = new
    ImageIcon(this.getClass().getResource("x.gif"));

    (above all on one line)

    Regards,

    Mark
    Mark, Sep 25, 2005
    #6
  7. Sameer

    Roedy Green Guest

    On Sun, 25 Sep 2005 10:38:03 -0400, Mark
    <> wrote or quoted :

    >private ImageIcon xGif = new
    >ImageIcon(this.getClass().getResource("x.gif"));


    to understand getResource, have a look at
    http://mindprod.com/jgloss/image.html

    It shows you all the various ways of finding an image.

    getResource looks in plausible places and gives you an URL to the
    resource where it finds it. I suggest debug dumping that URL until you
    feel confident you understand how this all works.

    When the resource is in a jar, getResource finds it in the jar under
    the name of the package for the current class as the member name and
    the resource name.

    The following is quoted from http://mindprod.com/jgloss/resource.html

    Here my recipe for accessing resources:

    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
    http://mindprod.com/jgloss/jarexe.html for how. Use winzip or similar
    ZIP utility to verify the package name and resource name are correct
    inside the jar, including case.


    In your class, access the resource with:

    // accessing a resource
    URL url = Entities.class.getResource( "entitytable.ser" );
    System.out.println( url );

    Note the lack of dots, slashes, package name or class name in the
    resource. Dump the URL out so you can double check where it found it.


    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Sep 25, 2005
    #7
  8. Sameer

    Sameer Guest

    What do we mean by a resource?
    Anything that is embedded in the jar file other than class file is a
    resource?
    How to refer to a resource?
    By the name of the file like this?
    this.getClass().getResource("x.gif"));
    Do we need to define a package to use a resource?
    What about files in directory other than current?


    What may be problem with general approach i have followed? Is this a
    problem related with jar file or jvm ?
    Sameer, Sep 27, 2005
    #8
  9. Sameer wrote:

    > What do we mean by a resource?
    > Anything that is embedded in the jar file other than class file is a
    > resource?


    class files and everything else are 'resources' in
    the way Mark (I presume) was referring to them.

    There are also other ways to refer to class files
    (like 'import'ing the class into another java file),
    but classes and GIF's and HTML files in the Jar file
    can all be thought of as 'resources'.

    Try running this code..
    <sscce>
    public class TestResource {
    public static void main(String[] args) {
    Object o = new Object();
    java.net.URL urlToObject =
    o.getClass().getResource("/java/lang/Object.class");
    System.out.println("urlToObject " + urlToObject);
    }
    }

    > How to refer to a resource?


    It depends on where it is, inside the jar file.

    For 'pretty.gif' in the 'root' of the jar file, you
    might find it like this..

    URL url = o.getClass().getResource("/pretty.gif");
    System.out.println("url " + url);

    Note the leading '/', and that you never use system
    specific path spearators like the windows '\' in
    getResource().

    For 'imgs/scenery/pretty.gif', ..

    URL url = o.getClass().getResource("/imgs/scenery/pretty.gif");
    System.out.println("url " + url);

    Note you should immediately check that you have a valid
    URL if you have problems, since 99%* of the time the problem
    is that the string given, does not locate the resource that
    the developer expects it to.

    * 86.3% of statistics are made up on the spur of the moment.

    > What may be problem with general approach i have followed? Is this a
    > problem related with jar file or jvm ?


    No. Once you get the hang of it, you should be able
    to get at your resources in or out of jar files.

    HTH
    Andrew Thompson, Sep 27, 2005
    #9
  10. Sameer

    Roedy Green Guest

    On 27 Sep 2005 00:33:23 -0700, "Sameer" <>
    wrote or quoted :

    >
    >What may be problem with general approach i have followed? Is this a
    >problem related with jar file or jvm ?


    please read the following entries:
    http://mindprod.com/jgloss/jarexe.html
    http://mindprod.com/jgloss/package.html
    http://mindprod.com/jgloss/classpath.html

    All classes and resources have packages except in toy demo
    applications.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Sep 27, 2005
    #10
  11. Sameer

    gbruno Guest

    I can get ImageIcons from my jar
    Is it possible to get a FileInputStream?

    I want to store short rtf files in my jar
    currently I get them via:
    FileInputStream fi = new FileInputStream(finamestring);
    rtf.read(fi, rtfeditor.getDocument(), 0);

    maybe FileDescriptor instead of string?

    I get my resources via:
    ClassLoader cl = ResourceAnchor.class.getClassLoader();
    Icon picon = new ImageIcon(cl.getResource(ficon));

    so I need to cast a url as a Fie=ledescriptor???
    gbruno, Oct 6, 2005
    #11
  12. gbruno wrote:
    > I can get ImageIcons from my jar
    > Is it possible to get a FileInputStream?


    Not to files within a Jar, but with an URL you can...
    <http://java.sun.com/j2se/1.5.0/docs/api/java/net/URLConnection.html#getInputStream()>

    > I want to store short rtf files in my jar


    Sure. I've put HTML in a har file, and Java components
    not only read the HTML, they also correctly load any
    linked stylesheets and images. Rendering the HTML and
    styles is another matter, but a JEditorPAne can sure
    load them!

    > currently I get them via:
    > FileInputStream fi = new FileInputStream(finamestring);
    > rtf.read(fi, rtfeditor.getDocument(), 0);


    Why not
    <http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JEditorPane.html#setPage(java.net.URL)>
    ?
    Andrew Thompson, Oct 6, 2005
    #12
  13. Sameer

    Roedy Green Guest

    On Sun, 25 Sep 2005 11:07:02 +0530, "Sameer"
    <> wrote or quoted :

    >I want to deploy my Java GUI application. I have created a jar file using
    >the command
    >jar cvfm icClient.jar manifest.mf *.class *.gif
    >When my application executes, the buttons in the applications which are
    >created using gif files wont displayed.
    >I checked that the jar file contains the gif files by extracting it.
    >If i put the gif files in the same folder as the jar file, the icons gets
    >displayed.


    Sounds like you ignored the package names or your classes and
    resources don't have package names.

    See http://mindprod.com/jgloss/jar.html
    http://mindprod.com/jgloss/jarexe.html

    There are many threads on this issue. You might revisit them with
    groups.google.ca
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Oct 6, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. umberto
    Replies:
    4
    Views:
    557
    umberto
    Oct 24, 2003
  2. Arnold Peters
    Replies:
    0
    Views:
    554
    Arnold Peters
    Jan 5, 2005
  3. muttley
    Replies:
    0
    Views:
    2,688
    muttley
    Oct 20, 2005
  4. cyberco
    Replies:
    4
    Views:
    3,722
    Roedy Green
    Feb 14, 2006
  5. Arnold Peters
    Replies:
    0
    Views:
    637
    Arnold Peters
    Jan 5, 2005
Loading...

Share This Page