0

I've seen this answer: Add image to JAR Java But I didn't understand how does it work. I'll try to explain step-by-step.

At the begining I add image to my project

  • test
    • src
      • com.test
        • tester.java
        • img.png
    • images
      • img.png
    • JRE System Library

My question - where should I place my img.png?

Then I try to get it from resource

URL url = tester.class.getResource(PATH);
     ImageIcon ii = new ImageIcon(url); 

I've tried different pathes like bellow:

  • "images/img.png" - it doesn't work. ii has wrong size and etc
  • "/images/img.png" - it doesn't work at all.

Could you tell me what I'm doing wrong? if it isn't difficult for you, please step-by-step.


Guys, I've taken note that if I have an url with large length, I can't see images, but if I copy file onto C:(for example), I can see it. What's happened? Is it bug? By the way, my url contains !!! symbols.

Community
  • 1
  • 1
Nolesh
  • 6,848
  • 12
  • 75
  • 112
  • 2
    Can you explain this: `it doesn't work. ii has wrong size and etc`? Does it load image rite but you are not able to display it in correct size? – Harry Joy Jan 09 '12 at 12:41
  • If I write wrong path it takes an error: Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.(Unknown Source) But if write correct path like image/img.png - it takes wrong ImageIcon with height=-1 and width=-1. So I didn't see the result – Nolesh Jan 09 '12 at 12:57
  • Seems like issues are with size of image and not image's path. – Harry Joy Jan 09 '12 at 13:02
  • I've taken note that if I have an url with large length, I can't see images, so I have the error above, but if I copy file onto C:(for example), I can see it. What's happened? Is it bug? – Nolesh Jan 09 '12 at 14:05

4 Answers4

5

/images/img.png is an absolute path because it starts with /. Based on your question, this path does not exists, because the images folder is in the test package, so it should be /test/images/img.png

The path images/img.png is relative to the class from you are trying to load the resource. The problem is, I don't understand what you mean by " it doesn't work. ii has wrong size and etc". You should have elaborated on that.

I won't describe you the rules of loading resources here, instead, I suggest you to consult the docs:

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
bpgergo
  • 15,669
  • 5
  • 44
  • 68
  • 1
    Your links point to the Javadocs of Java 1.4, which is really really old. – Eli Acherkan Jan 09 '12 at 14:01
  • It is best to link to the latest version of the JavaDocs. I have edited your answer to point to J2SE 7. For tips on getting a link to the latest docs, see [point 2 of advantages](http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7090875). (And feel free to vote for the RFE - which would make J2SE JavaDoc link-rot a thing of the past.) – Andrew Thompson Jan 09 '12 at 14:09
  • Thanks for fixing the docs links. I figured the classloader principles involved here has not changed since 1.4, but I understand this convention of linking the latest JavaDocs is a good convention and I'll try to respect this in the future. Thanks again – bpgergo Jan 09 '12 at 14:20
2

You should place your image in images folder.

and load the image like this

String path = "images/img.png";
URL url = tester.class.getResource(path);
Rajesh Pantula
  • 10,061
  • 9
  • 43
  • 52
  • It works when I compile it from Eclipse. But when I export it into JAR file - it doesnt work. I've checked path. It's look like this: /C:/test.jar!/com/test/images/img.png - I doubt about ! symbol after JAR exstention! – Nolesh Jan 09 '12 at 13:09
  • This is the path when you extracted the jar using "jar -xvf test.jar" ? how did you get that ! symbol? – Rajesh Pantula Jan 09 '12 at 13:23
  • No, I using standart Eclipse's export function. I get ! symbol in this code: URL url = ARMMain.class.getResource("images/BCircle.png"); This url contains ! symbol. I don't know why? – Nolesh Jan 09 '12 at 13:32
  • The `!` simply means a Jar (or Zip). The path to the image can be obtained using `"/com/test/images/img.png"` from *any* class in the app., but also by `"images/img.png"` from a class in the `com.test` package. UPDATE *"where should I place my img.png"* I just noticed you had it in two places! My advice applies to `/com/test/image/img.png` (which was neither of them). Adjust as needed, either can work. – Andrew Thompson Jan 09 '12 at 13:59
  • I've taken note that if I have an url with large length, I can't see images, but if I copy file onto C:(for example), I can see it. What's happened? Is it bug? – Nolesh Jan 09 '12 at 14:02
  • *"Is it bug?"* Not in the J2SE (99.99% certain). – Andrew Thompson Jan 09 '12 at 14:04
  • @Ares place the image in the images folder, having the image in multiple locations is confusing. you wont know from which location the image has been loaded. – Rajesh Pantula Jan 09 '12 at 14:08
  • It's ok when the url is shorter. By the way, my url contains !!! symbols like this file:/C:/MyProg!!!/test... Is it ok? – Nolesh Jan 09 '12 at 14:17
  • No it is not OK when you have special characters like ! in url path, they should be encoded, check for escape characters and url encoding. – Rajesh Pantula Jan 09 '12 at 14:22
  • I was right! If your path contains !!! symbols - it doesn't work!!! How can I do it? – Nolesh Jan 09 '12 at 14:29
  • rao_555 How can I check for escape characters and url encoding? – Nolesh Jan 09 '12 at 15:18
0

I tried all these answers for my problem, and none of them worked. I asked a friend and he answered my problem perfectly. Create a source folder called Images (ie if you're using eclipse, right-click your project -> new ->sourceFolder). Call it whatever you want, i called my Images. Put some images in it.

Now i had JLabels where i gave them ImageIcons. Look at the following code.

    ImageIcon BPawn;
    ImageIcon WPawn;
    JLabel Label = new JLabel[8][8] //2D array of labels that gives each spot a position.
    public void setPieces(){
        //set up pawns in their respective positions.
        BPawn = new ImageIcon("Images/BPawn.png", "BPawn");
        WPawn = new ImageIcon("Images/WPawn.png", "WPawn");
    for(int i=0;i<Label[0].length;i++){
        Label[1][i].setIcon(BPawn);
        Label[6][i].setIcon(WPawn);
    }//end for

    }//end setPieces.

There is a lot more in setPieces() method, but this glimpse is how you would reference the images in your source folder when you create an executable jar and want the images to show up.

A-a-ron
  • 68
  • 7
-1

you used in your path Images/BPawn.png again, do: BPawn = new ImageIcon("{Projectname}/{map}/{image.jpg/png/whatever}");

And the same for WPawn

Timo
  • 1
  • Welcome to stackoverflow. This does not seem to be an answer, but rather a comment for another answer, so it will likely be deleted soon. – Marco13 Jul 23 '17 at 02:15