views:

1222

answers:

4

Hi, I'm using some imgages in JFrame, I have given a path to load image into panel of the frame, but when i made jar of the application its not dispalying images. Where should I place the image? How should I specify the path?

setIconImage(Toolkit.getDefaultToolkit().getImage( "D:\RFT\src\dailogBox\dump.jpg")); like this i have done.

thanks in advance

A: 

There are many ways, for example make a folder called resources in your jar file and reference your images from there using a relative path....

hhafez
i have pasted image inside the source folder,how to set relative path?
A: 

Add the image in your jar as a resource, then (assuming the jar is on your classpath), use the class loader to load it as a stream.

joeslice
can u give me an example for this?
+6  A: 

First step: Put your image in a defined location in your JAR-file. If you put it into the src-folder, maybe your IDE or your build-tool will package it to the JAR automatically. Otherwise check the documentation of your IDE/build-tool, in which location you have to put it.

Second step: Access the file from your program. I assume you put it in the package (the path in your JAR) package1/package2 and the file is called dump.jpg. In that case you call:

setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/package1/package2/dump.jpg")));

getClass().getResource(...) returns an URL for a resource on your classpath. You start the path in the classpath with a '/' and use the complete path/packages to your resource, separated by '/'.

Mnementh
just solved this same problem for me. Thanks :)
nedlud
A: 

The easy way to do this is to have the folder your image is in parallel to the jar file. Example: Say in the code you call the image like this: pic = new ImageIcon("Pics/buttonpic.jpg");

This means your picture would be called buttonpic and is located in a folder called Pics in your project directory. Now after you make your jar file, copy that Pics folder into the same folder your jar file is located. Example: if your jar file is located on your desktop, then put the Pics folder on the desktop. Now when you run the jar file, your pictures will work correctly. Now to cover up the fact that you need to have your Pics folder next to your jar file, you can create a new folder and place in that folder your jar and the Pics folder. Then right click the jar and create a shorcut, then place that shortcut on the desktop or where you wanted to have just the jar file, and place the new folder you created whereever you want to hide it at.

EasyAnswer