Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2006

Tip: Looking for answers? Try searching our database.

Interesting loading image from jar problem

Thread view: 
JavaEnquirer - 22 Feb 2006 15:31 GMT
I have no problems loading an image from a jar using a class which
shares that jar. However, if I have a utility jar which contains a
ImageLoader utility class and use this class from my main application
jar which contains the images, I get a null pointer exception on
createImage.

( I'm using the utility class's class loader and calling the get
resource method. ) I don't get a problem running my application
jar-less in Eclipse.

When I got this error I assumed that by default you can only load a
resource from the same jar. So, I put the images into the utility jar
too, but to no avail - the same error.

Anyone got any pointers? Is there a way of loading resources from other
jars as I'm trying to do?

many thanks in advance.
Thomas Fritsch - 22 Feb 2006 16:17 GMT
> I have no problems loading an image from a jar using a class which
> shares that jar. However, if I have a utility jar which contains a
[quoted text clipped - 9 lines]
> resource from the same jar. So, I put the images into the utility jar
> too, but to no avail - the same error.
If you start your application with
    java -jar primary.jar ...
then Java loads classes and resources only from your primary.jar file,
and from the JRE's internal libraries. This is why your getResource(...)
call returned null.
(By the way: any "CLASSPATH=..." or "-classpath ..." is ignored, when
you use "java -jar ...")

> Anyone got any pointers? Is there a way of loading resources from other
> jars as I'm trying to do?
If you want Java to load classes and resources also from your secondaray
jar-file(s), then you have to specify these file(s) in the manifest
inside of your primary.jar file. For example:
  Class-Path: secondary1.jar secondary2.jar

See
<http://java.sun.com/j2se/1.4.2/docs/guide/jar/jar.html#Manifest%20Specification>
and look for "Class-Path:" there.

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

JavaEnquirer - 22 Feb 2006 17:15 GMT
The problem is jar1 contains images and is the main application jar.
jar2 contains a utility image loading method which gets called by jar1.
So, jar2 gets asked by jar1  to access images from jar1 and return them
to jar1.
Andrey Kuznetsov - 26 Feb 2006 10:23 GMT
> The problem is jar1 contains images and is the main application jar.
> jar2 contains a utility image loading method which gets called by jar1.
> So, jar2 gets asked by jar1  to access images from jar1 and return them
> to jar1.

then jar1 should provide InputStream (getResourceAsStream) to jar2

Signature

Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

Steve W. Jackson - 22 Feb 2006 16:24 GMT
> I have no problems loading an image from a jar using a class which
> shares that jar. However, if I have a utility jar which contains a
[quoted text clipped - 14 lines]
>
> many thanks in advance.

I'm not quite sure I truly understand what you say you're trying to do,
so apologies if I'm off the mark here.

We don't use a custom class loader in anything.  But when we load an
image file, it's usually done in one of two ways.  In many cases, we use
the Class method getResource with the complete path to the image (with
slashes rather than dots) like:  /com/company/images/a.jpg

In other situations, we use a static method in a utility class.  We pass
it a string just like that above.  It has a ByteArrayOutputStream that
it keeps.  It uses that object's getClass() to get a Class, then calls
getResourceAsStream() to get an InputStream, which it reads byte by byte
and writes to the ByteArrayOutputStream.  When finished, it creates a
new Image object using Toolkit.getDefaultToolkit().createImage(), with
the parameter to the latter method being baos.toByteArray().

We've had multiple people writing code over the years, so we're not
always consistent in which we use -- they're highly similar.  But
whichever way you use should work irrespective of what jar file they're
in if you have the complete path to the image -- barring any impacts I'm
unfamiliar with from the use of your own class loader.

HTH,
= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

JavaEnquirer - 22 Feb 2006 16:47 GMT
Apologies if I wasn't clear. I have two jars:

application.jar and utilities.jar. The first jar contains my
application, the second is a library to be reused by other
applications.

Application code in application.jar calls a utility method in
utilities.jar each time it wishes to load and image. ( This utility
method also uses a media tracker. ) However, the actual images as you'd
expect reside inside the application.jar in a folder called images.
This all works fine in Eclipse. In addition, when the code that
comprises the two jars is packed into one single jar, everything works
fine. The problem arises when two separate jaars are used. I do not
have any class path problems as application.jar can call all other
methods in utility.jar.

The problems seems to be the ping-pong of calls between the two jars
and the lack of visibility of the images in application.jar to
utilites.jar. I load the images using the following technique:

ClassLoader classLoader = ...
URL imageURL = classLoader.getResource(pathRelativeToClassRoot);
Image image = Toolkit.getDefaultToolkit().createImage(imageURL);

However, the above code found in utilities.jar needs to load images
from application.jar ( or any other jar ). It seems to me that
utilising a class loader associated with utilities.jar is going to be
of no use when attempting to access resources inside other jars.
Consequently, I give the ImageLoader code the class loader associated
with the relevant jar. However, this does not work.

I do not want ot go down the avenue of tightly coupling utilities.jar
to application.jar by adding application.jar to its class path.
utilities.jar shouldn't have any dependencies and should be re-usable.

1. application.jar ( containing images ) --- calls loadImage on ---
utilities.jar

2. utilities.jar --- loads an image from application.jar

3. utilities.jar -- returns the image to --- application.jar for
display.
hiwa - 26 Feb 2006 11:01 GMT
Show your contents/structure of your two jar files and
manifest entries for them. And make it clear that
accessor/accessee relationship between those classes
and resources in your description.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.