Images do not export when jar is created
I am trying to export a project using eclipse, and the Images that I have in a separate folder do not export. I have tried to use
image = new ImageIcon (Toolkit.getDefauleToolkit ().getImage("Image/image.png"));
and image = new ImageIcon (ImageIO.read (new File ("Image/image.png")));
and neither of them worked. It works when I run the program from eclipse, but not when I export. How could I fix this?
Thanks for the help.
sirlink99
Practically a Master Poster
661 posts since Oct 2010
Reputation Points: 45
Solved Threads: 19
Same problem was with me too... So i simply made a jar with libraries and put images in a seprate folder and made a extract.
Majestics
Practically a Master Poster
621 posts since Jul 2007
Reputation Points: 199
Solved Threads: 49
and neither of them worked.
Those techniques for reading image files expect the image files to be on a disk.
If you want to read the image "files" that are in a jar file, you must treat them as resources and use the getResource() or getResourceAsStream() methods to get a URL or input stream that can be read by the class the is reading the image.
There are lots of code samples here if you search for getResource
The getResource method uses the classloader's classpath so the method will also work when the files are on disk and the path to the files is pointed to by the classpath
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
When I tried to get the image using this line
cursor = new ImageIcon (Toolkit.getDefaultToolkit().getImage(getClass().getResource("Cursors/Cursor_Black.png")));
it gives me this error
Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(Unknown Source)
at sun.awt.image.URLImageSource.getDecoder(Unknown Source)
at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
at sun.awt.image.ImageFetcher.run(Unknown Source)
I have the images in the project folder in eclipse, where they run when using the previous codes I listed in my first post.
sirlink99
Practically a Master Poster
661 posts since Oct 2010
Reputation Points: 45
Solved Threads: 19
Try using the ImageIO read method vs the getImage method.
For debugging: Get the URL into a variable and print it out to verify the path is correct.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
I get a null when I print out the URL. would "Cursors/Cursor_Black.png" be enough, or would I have to put in an absolute path? My code currently looks like this
URL url = getClass().getResource("Cursors/Cursor_Black.png");
System.out.println (url);
cursor = new ImageIcon (ImageIO.read(url));
sirlink99
Practically a Master Poster
661 posts since Oct 2010
Reputation Points: 45
Solved Threads: 19
If your "Cursors" directory is not under your source folder, you may need to add that to your classpath as well.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
would I move it to the folder above the package, or in the package. And would it be in the src, or the bin section?
sirlink99
Practically a Master Poster
661 posts since Oct 2010
Reputation Points: 45
Solved Threads: 19
If you place it under src then it will probably automatically be on your project's classpath. I don't use Eclipse, so I can't verify that for you though.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
I originally had it in the project area. In the same area where the bin and the src options were located, and it worked fine there. it is just when exporting that is the issue.
sirlink99
Practically a Master Poster
661 posts since Oct 2010
Reputation Points: 45
Solved Threads: 19
basically this code works
src
---> myFolder
------------> myClassWhichLoadsImage
---> myFolder.Images
--------------------> someImage.jpg
then you can call
try {
image = imageIO.read(myClassWhichLoadsImage.class.getResource(
"Images/someImage.jpg")
} catch (IOException ex) {
Logger.getLogger(myClassWhichLoadsImage.class.getName()).
log(Level.SEVERE, null, ex);
}
frame.setIconImage(image);
mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
That's because you probably didn't specify that the images directory was to be copied into the jar when it was built. If you place it under 'src' then it will probably be included by default.
Your IDE project classpath is not necessarily the same as your jarred application.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
It currently looks like
Project ScreenRecorder
-->src
---->screenCapture (Package)
------>ScreenCapture.java
---->Cursors
------>Cursor_Black.png
and I tried your code and it didn't work for me.
If I were to add the images folder into the src would I have to create the file with eclipse, or would I be able to just copy and paste it into the src folder and then refresh the eclipse file explorer to bring it up?
sirlink99
Practically a Master Poster
661 posts since Oct 2010
Reputation Points: 45
Solved Threads: 19
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
The link that solved the issue is not working any more.
sirlink99
Practically a Master Poster
661 posts since Oct 2010
Reputation Points: 45
Solved Threads: 19
Yeah... keep reading down in the thread if you want to learn how to create a jar file from the command line if that's your issue?
If not, I created that splash screen app with the images in the same directory as the jar file I was executing.
So I shipped the .jar file in a folder with the images uncompressed.
Like I said, that seemed to reliably work across all platforms, but whether it is good practice nowadays I don't know?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
I have tried several times to make the jar file using the command line, but it doesn't seem to recognize my jar command. maybe I will reinstall JDK
sirlink99
Practically a Master Poster
661 posts since Oct 2010
Reputation Points: 45
Solved Threads: 19
What error messages do you get?
Also what OS are you on?
Also, I know this is bad practice, because you should really set your class paths, but what I did when I was learning to create jar files from the command line was to dump all my main files into the /bin folder.
That way I could just cd to the bin folder and compile the files without having to link elsewhere.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Have you tried adding a forward slash before the folder name when the directory is located within your src directory? i.e. getResource("/Cursors/Cursor_Black.png")
This is purely a pathing problem that you need to figure out with your build environment.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
Yeah, I'd echo the above, you don't need to figure out how to create jar files from the command line, especially if eclipse is doing it for you.
Also another thing you might try.
Sometimes change the png from lowercase to uppercase. As some apps save the files differently.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439