Problem with resources packed in a jar file

S

Sameer

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?
 
A

Andrew Thompson

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.
 
S

Sameer

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().
 
A

Andrew Thompson

Sameer said:
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.
 
M

Mark

Sameer said:
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
 
R

Roedy Green

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.
 
S

Sameer

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 ?
 
A

Andrew Thompson

Sameer said:
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
 
G

gbruno

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???
 
A

Andrew Thompson

gbruno said:
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...
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)>
?
 
R

Roedy Green

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top