1

In java i have to get the exact location of a image which is in a folder(Emoticon) under source directory inside a package but whenever i call

String s= (Main.class.getResource("Emoticons\\happy.PNG")).getPath();
System.out.println(s);
emotable.put(emo[0], s);

it gives me a invalid path like this

/E:/oop/java/emoti/out/production/emoti/sample/Emoticons%5chappy.PNG

There is nothing called %5c in the path.Can you tell me why it is coming or how to avoid this.

Shuvo
  • 29
  • 1
  • 5

1 Answers1

0

For getResource you need to use not a back-slash but a usual slash:

getResource("Emoticons/happy.PNG")
Maxim Votyakov
  • 714
  • 5
  • 10
  • But now after jar conversion the image disappears. – Shuvo Dec 09 '15 at 03:10
  • @Shuvo Because an entry in a jar file is not a file on the file system, which is what `Path` is used for. That's why `getResource()` returns a `URL`, not a `File` or `Path`, because it may not be one of those. To read content, use `getResourceAsStream()`, instead of *assuming* the resource is a file. – Andreas Dec 09 '15 at 03:19