Problem loading image in JAR

  • Thread starter Aloys Oberthuer
  • Start date
A

Aloys Oberthuer

Hello,
I am trying to load a .gif file from a JAR, and found the following way to do it

ClassLoader cl = this.getClass().getClassLoader();
Icon newIcon = new ImageIcon(cl.getResource("/images/icons/NewMan16.gif"));
// new ImageIcon(cl.getResource("images/icons/NewMan16.gif")); does not work either

The problem is, it does not work. The code is from class e.ar.Ear.someMethod(), the /images directory is top-level in
the JAR, i.e. the directory structure is as follows:

e/ar
images
properties


Strangely though, accessing the I18N files in directory /properties does work
ResourceBundle rbEarMenus = ResourceBundle.getBundle("properties/rb_Menus", getEarLocale());
strMnFile = rbEarMenus.getString("strMnFile");


what's wrong here,
Aloys
 
A

Andrew Thompson

Aloys Oberthuer said:
Hello,
I am trying to load a .gif file from a JAR, and found the following way to do it

ClassLoader cl = this.getClass().getClassLoader();
Icon newIcon = new
ImageIcon(cl.getResource("/images/icons/NewMan16.gif"));

I have never needed to use ClassLoader.getResource(),
I usually use Class.getResource() instead (less typing).

Are you _sure_ the leading slash is not the problem?

In fact, the JavaDocs state that Class.getResource()
calls the ClassLoader.getResource() after making some
additions to the string used (specifically mentions leading '/')
- try using Class.getResource() instead.

HTH
 
A

Aloys Oberthuer

Andrew said:
do it


ImageIcon(cl.getResource("/images/icons/NewMan16.gif"));

I have never needed to use ClassLoader.getResource(),
I usually use Class.getResource() instead (less typing).

Are you _sure_ the leading slash is not the problem?

In fact, the JavaDocs state that Class.getResource()
calls the ClassLoader.getResource() after making some
additions to the string used (specifically mentions leading '/')
- try using Class.getResource() instead.

HTH

--
Andrew Thompson
http://www.AThompson.info/
http://www.PhySci.org/
http://www.1point1C.org/

Yes I am, I tried it... to give a little more detail:
My JAR has the following directory structure, i.e. images is a top-level directory in the JAR and Ear.class is in
subdiretory ar of subdirectory e:

Ear.jar
/e/ar/Ear.class // this loads images
/images
/properties

This is my Manifest file
Manifest-Version: 1.0
Main-Class: e.ar.Ear
Class-Path: mmmysql_2011_bin.jar ibm.jar tpdfd121se.jar Serialio.jar jspComm.jar plastic-1.1.2.jar



Actually I use the following code in my Eclipse IDE
ClassLoader cl = ClassLoader.getSystemClassLoader();
URL imgURL = ClassLoader.getSystemResource("images/icons/NewMan16.gif");
btQckNew = new JButton(new ImageIcon(imgURL));

which does work in the Eclipse environment, but as I export things to a JAR, I get that dreaded NullPointer. Must have
missed some fundamental point, I presume.

Aloys
 
A

Andrew Thompson

Aloys Oberthuer said:
.....
which does work in the Eclipse environment, but as I export things to a
JAR, I get that dreaded NullPointer. Must have
missed some fundamental point, I presume.

Like trying what _I_ suggested.
 
A

Aloys Oberthuer

Andrew said:
JAR, I get that dreaded NullPointer. Must have



Like trying what _I_ suggested.

that's not right, I tried it of course. Unfortunately it does not help.
Aloys
 
W

wit

Uzytkownik "Aloys Oberthuer said:
that's not right, I tried it of course. Unfortunately it does not help.
Aloys

Try the following code IT WORKS!:
String imagePath="/"+imageDir+"/anImage.gif"
try{
BufferedInputStream bis = new
BufferedInputStream(getClass().getResourceAsStream(imagePath));
ByteArrayOutputStream buffer=new ByteArrayOutputStream(4096);
int b;
while((b=bis.read())!=-1)
buffer.write(b);
byte[] imageBuffer=buffer.toByteArray();
image = Toolkit.getDefaultToolkit().createImage(imageBuffer);
bis.close();
buffer.close();
MediaTracker tracker=new MediaTracker(this);
tracker.addImage(image,1);
tracker.waitForAll();
}catch(Exception e){
e.printStackTrace();
}

Wit
 
R

Raymond DeCampo

Aloys said:
Actually I use the following code in my Eclipse IDE
ClassLoader cl = ClassLoader.getSystemClassLoader();
URL imgURL = ClassLoader.getSystemResource("images/icons/NewMan16.gif");
btQckNew = new JButton(new ImageIcon(imgURL));

which does work in the Eclipse environment, but as I export things to a
JAR, I get that dreaded NullPointer. Must have missed some fundamental
point, I presume.

Another example of how IDEs can be evil...

:)

Ray
 
J

Jazz Jezebel

I was playing with this recently for a class called JarURL (worked nicely):
mail me if it's not clear how to adapt it.

public void actionPerformed(java.awt.event.ActionEvent e) {
try {
String file = "/netbeansTrial/README3.html";

// start with "/" for different package in jar file - else package is
prepended OK
//java.net.URL url = JarURL.class.getResource(file);
// System.out.println("File name as URL: " + url);

jEditorPane1.setPage(JarURL.class.getResource(file));
} catch (Exception e1) {
System.out.println("Problem with URL" + e1);
}
}

See:
http://developer.java.sun.com/developer/qow/archive/76/index.html


See URLConnection in API (syntax = jar:jar path!path in jar)
 

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

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top