5

I wanted to test having a program with a simple png image on it. I wrote a short program that does this, but I can't seem to get the path right. I have checked, checked again, rechecked, and quadruple checked my path name as to not get it right, but this image will not display, no matter what I do. I used a short class wrote by Oracle in the ImageIcon documentation (the creaetImageIcon()) to accomplish this, but it doesn't seem to help. I'll post the entire program below, as it is very short.

package practiceImages;

import java.awt.BorderLayout;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ImageIconGUITest {

    public static void main(String[] args) {
        ImageIconGUITest gui = new ImageIconGUITest();
        gui.display();
    }

    private ImageIcon createImageIcon(String path, String description) {
        java.net.URL imgURL = getClass().getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    private void display() {
        JFrame frame = new JFrame();
        JLabel label = new JLabel(createImageIcon(
                "Users/Evan/javaItems/Sprites_and_Other_Art/green.png", "the color green"));

        frame.add(BorderLayout.CENTER, label);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2398233
  • 137
  • 1
  • 2
  • 10
  • Firstly, you've supplied a relative path, so the system is looking for the image relative to the location you executed the program. Secondly, the path should have a drive spec or at least a leading `/`. Depending on your setup, something like `C:/Users/Evan/javaItems/Sprites_and_Other_Art/green.png` should work (you may need to change the drive spec to meet your system) – MadProgrammer May 19 '13 at 05:10
  • @MadProgrammer Can you go into more detail about a relative path? What does that mean? Also, adding the drive spec dind't work. – user2398233 May 19 '13 at 05:13
  • 1- Make sure that the file exists in the specified location `System.out.println(new File("C:/Users/Evan/javaItems/Sprites_and_Other_Art/green.png").exists())` should return true, other wise the file is in the wrong location. 2- A relative path basically means a path location relative to the programs execution. So, if you were running the program from `C:/Program Files/MyAwesomeApplication` for example, a relative path of `Users/Evan/javaItems/Sprites_and_Other_Art/green.png` would actually look like `C:/Program Files/MyAwesomeApplication/Users/Evan/javaItems/Sprites_and_Other_Art/green.png` – MadProgrammer May 19 '13 at 05:23
  • You can test this by using System.out.println(new File("C:/Users/Evan/javaItems/Sprites_and_Other_Art/green.png").getAbsolutePath())` which will give you the full path. – MadProgrammer May 19 '13 at 05:24
  • @MadProgrammer ^Might wanna post this as an answer. – user2398233 May 19 '13 at 05:26
  • Done, hopefully also cleaned it up... – MadProgrammer May 19 '13 at 05:31
  • @MadProgrammer How do I get the new `C:/Program Files/insertAppNameHere` to get there? Do I just make a new folder, or what? – user2398233 May 19 '13 at 05:32
  • @user2398233 : This [answer](http://stackoverflow.com/a/9866659/1057230) too, might can guide you in the given direction – nIcE cOw May 19 '13 at 05:33
  • It's just an example. When you run your program, you can actually do `System.out.println(new File(".").getAbsolutePath())` and it will dump the current directory the application is running in. The problem is, Java is then appending your `Users/Evan/javaItems/Sprites_and_Other_Art/green.png` path to the end of this and is trying to find the file in a location that does not exist – MadProgrammer May 19 '13 at 05:34

6 Answers6

10

The getResource(String) method will only find resources that are on the run-time class-path of the application. Since this image seems like an application resource (i.e. supplied by you as part of the application) it should be put on the run-time class-path.

E.G. Most IDEs have a place you can put resources within the project structure, that will automatically be included at run-time. Move (or copy) the image to that path.

Then it becomes a matter of providing the correct String. Let us imagine your project is set up something like this:

  • bin
  • src
    1. com
      • our
        1. Application.java
    2. resources
      • green.png

So Application.java is in package com.our;, while the image is in the path resources/green.png.

If accessing the image from the Application, the correct path would be (drum roll please..)

"/resources/green.png"

Notes

  1. The leading / is important. It tells the JRE we want to look for the image from the 'root of the class-path', as opposed to using a path relative to the package of the class itself.
  2. Correct case is also vital. A string of "/resources/green.png" will not locate an image named "/resources/Green.png" or "/resources/green.PNG".

Eclipse paths

  1. Right click on the src directory, select Properties at the bottom of the menu.
    Eclipse properties
  2. Navigate (using the normal way you'd use without Eclipse) to the directory of the Location. Eclipse properties showing project Location
  3. Then go to the parent directory.
  4. You should see a bin directory that contains classes and (hopefully) the image.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • @MadProgrammer If you liked that, check out the [embedded-resource info. page](http://stackoverflow.com/tags/embedded-resource/info) which me and trashgod have updated to being a very Java specific info. page. ;) – Andrew Thompson May 19 '13 at 05:41
  • *"didn't work, but oh well"* We'll need more & better information that tha, if we are to have any chance of solving this. E.G. what exact steps did you take to try it? What IDE (if any) is being used? What is the listing of the `bin` (I think that is the Eclipse runt-time) directory? – Andrew Thompson May 19 '13 at 05:44
  • @AndrewThompson Console sent: Couldn't find file: /resources/green.png Did everything you said, read it over a few times, and it seemed logical. Not sure what the hell is going on. – user2398233 May 19 '13 at 05:46
  • *"Couldn't find file:"* That would be better stated as `Couldn't find resource` since.. by time of deployment (or possibly after being built) it will no longer **be** a `File` so much as an entry in a Jar. You still have not given me the listing of the `bin` directory. Note that the `.java` files are in `src` but the classes **(and run-time class-path)** is typically in `bin`. Make sure the image made it to (was copied to) the `bin`. – Andrew Thompson May 19 '13 at 05:55
  • @AndrewThompson What do you mean by "the listing of the bin directory"? I don't know what that means... – user2398233 May 19 '13 at 06:03
  • 1) Right click on the `src` directory. 2) Select `Properties` at the bottom of the menu. 3) Navigate (using the normal way you'd use without Eclipse) to that directory. 4) Then go to the parent directory. 5) You should see a `bin` directory.. – Andrew Thompson May 19 '13 at 06:07
  • @AndrewThompson Step 3: "Navigate (using the normal way you'd use without Eclipse) to that directory." What directory? When I selecte preferences, there's just a Resource and a Run/debug settings tab. I don't know what you're talking about anymore or why I'm doing this? – user2398233 May 19 '13 at 06:12
  • *"When I selecte preferences"* I told you to select **`Properties`**. I have expanded my explanation in the edit to answer.. – Andrew Thompson May 19 '13 at 06:37
  • 1
    Note also: This situation reminds me of why new programmers **should not** use an 'automagic' IDE. The minute something happens that is not according to plan, leads to complete confusion simply because you've not yet mastered the concepts of run-time and compile time class-paths (and the difference between them). If not the class-path, it will be something else. Maybe you should put the IDE aside for the moment and use a simpler editor (e.g. TextPad) for creating the source. – Andrew Thompson May 19 '13 at 06:49
  • @AndrewThompson When I go to the parent folder of the resources folder that I made, that's src. My resources folder, although I put the image into it, can not be opened, presumably because there is nothing in it, even though I put the green.png file into it through my workspace. – user2398233 May 19 '13 at 17:10
4

Firstly, you've supplied a relative path, so the system is looking for the image relative to the location you executed the program.

Secondly, the path should have a drive spec or at least a leading /. Depending on your setup, something like 'C:/Users/Evan/javaItems/Sprites_and_Other_Art/green.png' should work (you may need to change the drive spec to meet your system)

Thirdly, make sure that the file exists in the specified location, System.out.println(new File("C:/Users/Evan/javaItems/Sprites_and_Other_Art/green.png").exists()) should return true, other wise the file is in the wrong location.

A relative path basically means a path location relative to the programs execution. So, if you were running the program from C:/Program Files/MyAwesomeApplication for example, a relative path of Users/Evan/javaItems/Sprites_and_Other_Art/green.png would become an absolute path of C:/Program Files/MyAwesomeApplication/Users/Evan/javaItems/Sprites_and_Other_Art/green.png. This describes the path from the root location to the file/folder in question.

You can test this by using System.out.println(new File("C:/Users/Evan/javaItems/Sprites_and_Other_Art/green.png").getAbsolutePath(‌​)) which will give you the full path.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Using this fixed it for me:

JButton btnBanana = new JButton("New button");
btnBanana.setIcon(new ImageIcon("D:\\Android\\Company\\images\\bananas-icon.png"));
Pokechu22
  • 4,984
  • 9
  • 37
  • 62
0

use double slash instead of one , i had this problem and i fixed it . ill show you an example :

public Driver (){

    ImageIcon us = new ImageIcon("C:\saeed.gif"); // OS cant find it 
    ImageIcon uk = new ImageIcon("C:\\saeed0.gif"); // OS can 


  JButton button = new JButton ("Click here "  , us ) ;

  button.setRolloverIcon(uk);

  add(button);
}
0

To get the path of a image to a text filed, this code will help you

txtPath.setText(lblImage.getIcon().toString());

//txtPath is the text filed use todiplay the path //lblImage is the label which shows the image

Robert
  • 5,278
  • 43
  • 65
  • 115
sonic
  • 1
-2

You need to do C:\\Test\\test.png and not C:/Test/test.png

αƞjiβ
  • 3,056
  • 14
  • 58
  • 95
Vinni
  • 1