aspose file tools*
The moose likes Java in General and the fly likes Adding images to Jar Files (NetBeans) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Adding images to Jar Files (NetBeans)" Watch "Adding images to Jar Files (NetBeans)" New topic
Author

Adding images to Jar Files (NetBeans)

Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
Hi,

I want to include an image in a programs Jar file (using NetBeans) so that the program can run on any computer. The image I want to include is in a seperate folder from the source code: src\images\Image0.jpg.

If, in the program code, I specify the complete path of the image, then the Jar only executes as long as the image does not move (so if I specify the complete path as C:\User1\Desktop\Image0.jpg, and then delete the image from the desktop, the Jar doesn't run).

If I specify the image path as relative, such as src/images/Image0.bmp, then the program runs in NetBeans, but the Jar doesn't run at all.

Could someone please outline how this is done in NetBeans?

Thanks.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I don't use Netbeans so I'm not sure what part is specific to that IDE. Also, you say that when including the images in the JAR, "the Jar doesn't run at all" but that is not useful information. What goes wrong?

If I understand correctly you have a Jar that contains both the code and resources.
Resources (ignore the fact that it is in a JAR, for the moment) that are on the classpath can be accessed by the ClassLoader.
When the resources are on the classpath you can often get a java.io.File reference to the resource. If you have never done so, I recommend writing some code to play with this idea.
Now obviously you cannot get a File reference to a resource inside a JAR, but if you explore the ClassLoader API you'll see a way to get a reference to the resources as a java.net.URL.

All very interesting, but I suspect in your case you'll want to use getResourceAsStream(...), but it is worth playing with the concepts above since they'll help avoid problems you'lll have if you try to use getResourceAsStream from the start.
Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
Hi

Thanks for your reply. I'm trying:


But url reads null after getResource().
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

The slashes in the url path should be forward slashes, not backslashes.Check out the tutorial and ths source code linked from it and your problem should be solved.
http://download.oracle.com/javase/tutorial/deployment/jar/build.html


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

I doubt that the src folder will be on your class path, or in your JAR file, will it?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
Hi

@Darryl Burke:
I've corrected it:

But url still reads null.

@Rob Prime: I've added the src folder as a compile-time llibrary in NetBeans. Is this what you mean?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

No I don't. Resources must be either in the class path or inside JAR files on the class path. Your src folder is not used in either way. Copy the images folder to your bin folder and it should work.
Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
Rob Prime wrote:Resources must be either in the class path or inside JAR files on the class path. Your src folder is not used in either way. Copy the images folder to your bin folder and it should work.

The directory structure of the project is:

Which one is the bin folder?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

In this case it would be the classes folder. This holds all your classes in the package structure. I'm used to using "bin" for that, being an Eclipse user.

Make sure that the images are inside the classes/images folder, then use "/images/img.gif" as your resource path. images will be in the root of the JAR file so it will also be the root of your resource path.
Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
Thanks, it works now.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

In NetBeans, any resources you place in the src tree will be automatically copied over to the classes tree when you do a Build (or Clean and Build), except those files matching the "Exclude From JAR File" skeletons specified in Project -> Properties -> Build -> Packaging.

Note that the entire build tree, which contains the classes folder, will be emptied out whenever the project is next Clean-ed. If the resource is no longer available in the src tree, it will vanish from the classes tree too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding images to Jar Files (NetBeans)
 
Similar Threads
problem loading images
about splash screen with jar file...
ANT: Getting resource files moved to classes folder for JAR
Displaying images dynamically from database
Problems running jar file