My images will not load when running a JAR file exported from Eclipse.
I have the images in a resources class package. I've tried a images source folder as well with no luck.
Works perfectly when loaded from Eclipse. The images are in the exported JAR file, so they're exporting fine.
I've tried:
label.setIcon(new ImageIcon(MainFrame.class.getResource("/resources/header.jpg")));
I've also tried:
URL url = getClass().getResource("/resources/header.jpg");
Image image = Toolkit.getDefaultToolkit().getImage(url);
label.setIcon(new ImageIcon(image));
And:
try
{
label.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/resources/header.jpg"))));
}
catch (IOException e1)
{
e1.printStackTrace();
}
Any suggestions?
Works fine for me. Check what you may have different.
Example 1: (resources in src)
Steps:
File Structure
Code
[Right click on project] → [Export] → [Runnable Jar File] → [Set up Launch config]
Profit
FYI, the same setup runs in eclipse just fine also
Example 2: (resources not in src - but in project)
Steps:
File Structure (notice resources looks like a plain folder)
What we have to do now, is put the resources on the build path. What this does is put everything in the folder (excluding the folder itself) on the classpath
Right click on the project and go to [Build Path] → [Configure Build Path]
From the [Sources] tab in the dialog, select [Add Folder] and in the new dialog, select the [resources] folder
Now the contents of the resources folder is in the build path (notice the little package in the folder now
New code no longer uses the resources prefix for the path
Same as Step 3 and 4 from above, and profit!
UPDATE
Setting up Launch Configuration
Generally, once you run the class (i.e. Right click on class and Run as Java Application), a run configuration will be set up. You will need this to set as the launching point in the manifest. But here's how to do it manually.
Steps:
[Right Click Project] → [Properties] → [Run/Debug Settings]
You can see that I already have a run configruation (that is implicitly set from simply running the class). But to create a new one, select [New] → [Java Application]
Create a name for run configuration and browse or type an main launching class. In my case its the
com.stackoverflow.test.Main
classNow when you export as shown in the above example, you select the run configuration
Run the jar like above.
EDIT
Result to Check for
Manifest:
Extracted jar:
I have same issue.. You can use this..
Here WE4 is my class constructor name. Hope it helps.
Unless you have to have you files in the jar, this is probably the simplest way of doing it:
header = ImageIO.read(new File("./resources/header.jpeg"));
header has to be an Image/BufferedImage. This goes to the folder that the runnable jar is within and looks for a folder called resources. http://i.stack.imgur.com/x8xtO.png
Two Simple steps:
1 - Add the folder to Build Path;
2 - Use this:
For creating a runnable JAR file from Eclipse we may refer to the article "Creating Runnable Jars in Eclipse (or other IDE...or by hand):" (https://www.cefns.nau.edu/~edo/Classes/CS477_WWW/Docs/RunnableJarsinEclipse.html), it mentioned that we need do four things as
But for image file in the example code of above article it only creates the ImageIcon, it does not create the SWT Image, and there are many questions in the Internet for how to get SWT Image from URL or how to convert ImageIcon to SWT Image, below is the example code for getting the SWT Image from URL,
Another work around is if you put your resource file, an your .jar file in the same location, it should work. The only drawback is you have to have the resources with the jar file at all times.
If that's not an issue you can do it this way.