Search code examples
javaclassgetresource

Invalid path by getpath Function


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.


Solution

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

    getResource("Emoticons/happy.PNG")