Swing application and it's Images are running fine in eclipse. but when i run from jar file, images not displaying. I tryed to load images in different way, but some time jar file not executing.
My image folder is inside of source folder.
I load my images with:
1.Running fine in eclipse, but not in jar
ImageIcon right = new ImageIcon("./chack img/Green Tick 1.PNG");
2.Running fine in eclipse, but jar file not executing.
ImageIcon wrong = new javax.swing.ImageIcon(getClass().getResource("/chack img/Red Cross1.PNG"));
I tryed some other code also, still my issue not solved.
Put your images in the src folder, along with your classes. Optional, but make a package that stores your images. Compiling a jar file will not keep any directories that are not a source directory.
Also, remove all spaces from your folders and file names. This prevents confusion, and is likely part of your problem. No spacing is a good practice to follow in programming, and in most situations of naming folders and files.
Also, kiheru is correct, you should not have capital letters in the file extensions, for the same reason you should not have spaces; It can cause confusion and it is a bad practice in most cases.
EDIT: Simmilar Question
Related
This question already has answers here:
Java images not appearing in JAR file [duplicate]
(4 answers)
Closed 2 years ago.
I couldn't find an answer that works so I had to ask here. I have splashscreen file with the contents
JLabel image=new JLabel(new ImageIcon("./image/risk.jpg"));
And the folder structure looks like this
But for some reason when I go to create a jar file like this
The images don't appear in the jar file. I have looked at many other answers and they don't seem to work. I would greatly appreciate an expert eye to tell me what I am missing. Thanks in advance
new ImageIcon("./image/risk.jpg")
The images don't appear in the jar file
Non sequitur. Whether the image file is in that jar or not is irrelevant; this does not work with jar files, period.
That ImageIcon constructor takes in a file argument. File, here, is to be taken literally: It refers to a path to a file on your actual disk. You go with ., meaning: Current working directory, and thereby have lost all chances at a stable app; current working directory is whatever the JVM got started from and therefore unreliable. Even if you wave a magic wand and solve that problem, the next more pressing issue is that there is no risk.jpg file anywhere - it's an entry in a jar file, which is not, itself, a file. Merely an entry in a jar.
The solution
The solution is not to use that constructor, and instead to rely on java's resource retrieval system:
class MyClass {
public void foo() {
URL url = MyClass.class.getResource("risk.jpg");
}
}
This gets you a URL that represents a resource (could be an actual file, but could also be an entry on disk, the web, from a database, generated on the fly - whatever the classloader is providing. If you didn't mess with custom classloaders, it will always be either a file on disk, or entry in a jar file), and this can also be passed to ImageIcon (it also has a constructor that takes a URL).
This then looks for a file named risk.jpg which is sitting in the same place that MyClass.class is sitting. Whether that is on disk someplace, or in a jar file (so if this is in package com.foo;, you have a jar file with entry com/foo/MyClass.class, and that command would then find the entry com/foo/risk.jpg in the same jar file. You can use a prefix slash to go from the root of the jar: MyClass.class.getResource("/image/risk.jpg") (note, no leading dot!) will look in that jar for entry image/risk.jpg.
Great! Uh, but, that image should be in the jar, right?
Yes, this code will ensure you look in the same place class files are, which is good, but you still need to ensure the build is set up properly. 'let eclipse make a jar' is not a recommended build solution; invest some time in learning maven or gradle. However, if you insist, eclipse should just include them, but put the jpg in a source dir, right next to your java file (eclipse knows that non-java files are to be copied over, and java files are to be compiled).
In other words, if you insist on using eclipse's 'make a jar' as build tool, image has to move into src.
Alternatively, set up a basic maven project, where source is in src/main/java and image files and the like are in src/main/resources, use the maven plugin in eclipse to read the maven based project in, and that works too, if you want these directories fully separated for some reason.
I am a novice programmer trying to create a jar file that people can download and demonstrates a...JRPG-ish upgrade system I have. As a little visual pop, I want the central button to display a little icon as an extra visual pop. While it works fine in Eclipse, when I export it to a JAR file the icon is no longer displaying (though the rest of the program works as intended.
I have managed to make Eclipse import the files I want into the JAR file, as seen below.
This is the code I currently have to display them in Eclipse
imgDin = new ImageIcon("icons\Mark_of_Din.png");
imgNayru = new ImageIcon("icons\Mark_of_Nayru.png");
imgFarore = new ImageIcon("icons\Mark_of_Farore.png");
How can I modify this code to display them inside the JAR file?
You should be using
new ImageIcon(getClass().getResource("/Mark_of_Din.png"));
That little package icon in the icons folder icon, means the files in the icons folder will be at the root of the classpath. getClass() gets the calling class, and getResource() locates a resource relative to the class. The / brings the search to the root.
You can extract the jar and you will see the icons folder is not there.
See Related Post
"While it works fine in Eclipse.."
That's because when you pass a String to the ImageIcon constructor, it is looking for a file on the file system, relative to the working directory. In Eclipse (and most IDEs), the default working directory is the project root, and that's where your icons folder is.
CONTEXT FOR MY PROBLEM
I am creating a Project using java swings on netbeans IDE 7.1.2 plateform on
windows m/c.Then I'm executing the jar file created on windows m/c in a linux m/c(cent os).
In a part of my project i needed to display some pictures in run time.
PROBLEM STATEMENT
Case 1)
Now what happened is that in run time i brought those pictures from some other location to src directory of my project by executing cp(copy) command through java code.
And yes i have checked those pictures were copied inside src directory successfully.
After that when i tried to display those images in a label,pictures were not being displayed.
So i wondered why is that happening.
Case 2)
After this problem i made a change and i imported those pictures inside src folder of my project before "cleaning and building" the project on windows m/c instead of bringing them in run time on linux m/c.Then after this when i executed the jar file on linux m/c,pictures were getting displayed in label.
So only change i made was that i included the pictures in src directory during "cleaning and building" my project on windows m/c.
So what i don't understand is that
why pictures are not getting displayed in 1st case.
MY QUESTIONS AND DOUBT
1) In context to resources like files and pictures(which are being used in project and i'm not talking about source codes of project here),what exactly happens when a jar file is created ? Are those resources(files & pictures) also included in jar while building the project or not?
2) Why i'm facing such a problem ?(And i have also checked the permissions on those image files.They were exactly same in both cases.)
Any insights that any of you can provide will be deeply appreciated.
So I assume you ar doing some thing like
label.setIcon(new ImageIcon("src/path/to/image"));
or
BufferdImage img = ImageIO.read(new File("src/path/to/image"));
or worse, using absolute file references...
Netbeans will add the content of of your src directory to the resulting jar file when the project is built (excluding your source files ;)), this changes the way in which these resources need to be treated, you can no longer treat them as you would if they were files on the file system.
Once embedded within your application context (such as been included in your jar file), they become what is commonly known as embedded resources.
Instead, you need to use Class#getResource or Class#getResourceAsStream depending on your needs, for example...
label.setIcon(new ImageIcon(getClass().getResource("path/to/image")));
Updated with after more details
Image im=newImageIcon(this.getClass().getResource("/home/aman/Desktop/diya.jpg")).getImage();
Is the wrong approach for his type of file. This file is a file on the file system, instead you should be using something more like...
BufferedImage image = ImageIO.read(new File("/home/aman/Desktop/diya.jpg"));
jLabel1.setIcon(new ImageIcon(image));
Note, ImageIO.read throws an IOException, which will be expected to catch, this is useful for diagnosing missing files or bad image formats
Unless you have previously changed the visibility state of the label, there is no need to call setVisible, in fact, you're setting label invisible anyway...
I am having trouble loading Images contained within a JAR file. I have read a number of other posts related to this. But cannot find the answer. Now here is where it gets complicated.. This all works fine if I am using a Runnable JAR file exported from Eclipse and Run it using the standard JRE. However This JAR file is actually a plugin for a piece of software called pro/ENGINEER which has it's own JRE that is used to run the JAR file. The strange thing is.. that this works fine on the development machine, that has eclipse installed and so on, but doesn't work on any of the client machines?! But I can't see what is different.
I am trying to load the image using:
ImageIcon icon = new ImageIcon(this.getClass().getResource("resources/Header.png");
This method is called from my 'Start' class, and the Jar is structured as follows:
load
Start.java
load.resources
Header.png
If I Open the Jar file with WinRar you can see the Image definitely exists in the jar, in this position.
I know this may be a very specific question but if anyone can be of any help that would be great.
I did not have the particular problem you're having now, however I ran into the problem of not finding a resource in a JAR file several times in the past.
The solution was to not get the resource from the class, but from the class loader as demonstrated below:
ImageIcon icon = new ImageIcon(this.getClass().getClassLoader().getResource("resources/Header.png"));
Mind the .getClassLoader() call after the getClass().
Hope this helps.
This question already has answers here:
I'm trying to export a runnable jar in Eclipse, but I need it to include some other files necessary for running my program
(3 answers)
Closed 8 years ago.
When I run my program from eclipse, it shows up fine, the images from Resources\ show up, as do the sounds from the same place, and the text files are found properly. However, when I export my jar, and copy the resources into it with 7Zip, the images will work, but the sounds and the text files can't be found, even though they're in the same folder, with the same path used to find them in my code. I can fix this by putting a folder next to the jar file named Resources, and put everything in there, but I'd like to know why just putting it in the jar file only worked for the images, and how I can get it to work with the text and audio files as well.
An example to show you what I mean:
File inventory = new File("Resources/inv.txt");
threadpath = "Resources/threads.wav";
enemy1 = new Sprite(new Texture("Resources/miniForestGolem.png"));
When I run it in eclipse, all three work fine, but when I export it, and put the resources folder in the jar file, only the image works.
Edit:
I know how to include my resources, and have done so, I'm asking about how/why some of the resources aren't able to be accessed, even after adding them in.
Ok, from your comments we can infer the difference between executing it from eclipse and executing it from a .jar.
From eclipse: it works, because all that new File(...) find an actual file in Resources/
From the .jar: it won't work, since there is no file in a relative ./Resources/ path from the execution path of the application.
The way to make it work is the next:
Make sure Eclipse recognizes Resources/ as a source folder (right-click on project properties, Java Build Path, and add it as a source path)
Look for a replacement for your API methods that, instead of File objects, use InputStreams. Once you have it, retrieve all your resources as InputStreams taken from the classpath. If you are inside MyClass.java, do this: MyClass.class.getClassLoader().getResourceAsStream("Resources/inv.txt"), etc.
What you have achieved by doing this: instead of File objects built on actual operating system files, you will be retrieving InputStreams read straight from your java application classpath. This way, you can package them into a jar, or into a WEB-INF/classes directory inside a web application, or a library folder in some application servers... wherever you like as long as it is into the application classpath. I would do this if I had to package your application in a portable and usable way.