wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Problem getting image from JAR file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem getting image from JAR file" Watch "Problem getting image from JAR file" New topic
Author

Problem getting image from JAR file

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I have a JAR and it contains a directory called icons that contains all my image icons for my application.
I am running jdk 1.4.0 and here is the code that is compiling but not running:

Now when I run the applicationa and it gets to this point in the program to load the image this is the error that I get

Can someone help me see the light here? Thanks. If you need any more information let me know.
It is telling me this nullpointerexception on the line Image image = getClass().......
Thanks again.


GenRocket - Experts at Building Test Data
John Smith
Ranch Hand

Joined: Jul 20, 2001
Posts: 84
This is a slightly educated guess, but try replacing the line:

with

Also, make sure your jar file is in the classpath and that inside of the jar file, the file icons/connect16.gif really exists. I had problems getting images out of a jar file a while back and the slash at the beginning (just before icons) did the trick for me. Let me know how it goes...
Randy
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Randy, thanks for the guess, but that is not the problem. What I should mention is the fact that it DID work under jdk 1.3 and now with jdk 1.4 it does not.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Found a bug report on Sun's site that may be your problem... looks like you might have to parse the URL to get the real path to your file...

-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

That pretty much sums it up Nathan. That pretty much scroggs either me using jdk 1.4 or from storing images in my jar file. I am not going to parse my URL the way they suggest. Seems like too much overhead. I think I will just store the images externally.
Thanks for your reply Nathan. You always have the answers.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem getting image from JAR file
 
Similar Threads
Problem with adding a Label to a Button
Problem: Adding ejb jar
Problem with JDK 1.4
web browser JTextField problem
Strange exception when mouse passes over JEditorPane's scrollbar