Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

JAR Question

  Asked By: Bertha    Date: Jul 29    Category: Java    Views: 467

I made a application and put all *.class files into a JAR file. It works
fine, the only thing I couldn't do alone is load a image (for example) file from
inside of this JAR. If I tell to load something from ./ it try to load from the
path location of the jar file. How do I do this?



2 Answers Found

Answer #1    Answered By: Hababah Younis     Answered On: Jul 29

You have to use the method
getClass().getResource(String name)

This returns a java.io.URL
This method is inherited from Object, so you can only
use it in an instance context. Within a static context

you have to use

ClassLoader.getSystemResource(String name)

Answer #2    Answered By: Edfu Massri     Answered On: Jul 29

Have you tried using the getResource function?
If you do something like this:

public class  Class1 {
URL imageURL = this.getClass().getResource("image.gif");
ImageIcon icon = new ImageIcon(imageURL);

the file  called image.gif is assumed to be located in the root
directory of the jar  that contains Class1.class.

Didn't find what you were looking for? Find more on JAR Question Or get search suggestion and latest updates.