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.

Edited 5 Years Ago by peter_budo: Please use code tags for any code you post

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.

Edited 5 Years Ago by Majestics: n/a

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

Edited 5 Years Ago by NormR1: n/a

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.

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.

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));

If your "Cursors" directory is not under your source folder, you may need to add that to your classpath as well.

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?

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.

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.

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);

Edited 5 Years Ago by mKorbel: n/a

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.

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?

Seems safer to add the images as files. But don't quote me on that, haven't use java in a while so best to take the advice of the gurus here.

Also, looks like you're another victim of the IDE, you should ideally know how to create a jar file from the command line.

I wonder if the following is still legit?

http://www.daniweb.com/software-development/java/threads/37189

That was back in the day.

I think I packaged up my jar files like so...

http://www.daniweb.com/software-development/java/threads/39461

Edited 5 Years Ago by iamthwee: n/a

The link that solved the issue is not working any more.

Edited 5 Years Ago by sirlink99: n/a

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?

Edited 5 Years Ago by iamthwee: n/a

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

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.

Edited 5 Years Ago by iamthwee: n/a

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.

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.

Edited 5 Years Ago by iamthwee: n/a

I used forward and 2 backslashes (to escape the escape character) and they both worked. I have not tried changing .png to .PNG. I will try now, but it did work with the lowercase.

This article has been dead for over six months. Start a new discussion instead.