I have a function:
public static ImageIcon GetIconImageFromResource(String path){
URL url = ARMMain.class.getResource(path);
return new ImageIcon(url);
}
It works great, but if I place my .JAR file onto for example C:\Ares!!!\myfile.jar it fails and I don’t see my icons. I’ve observed that my URL is file:/C:/Ares!!!/myfile.jar!/com/test/images/img.png. So after JAR extension there is the ! symbol! I think this is the main reason! What can I do to avoid this problem? Thanks.
This is apparently a problem (more than a bug) of Java. The JAR URL uses
!/as delimiter, but does not escape it when it appears inside the file path.You can try to escape it yourself, turn
Ares!!!/intoAres!!%21/, but keepmyfile.jar!/as is.According to RFC3986,
!is a reserved sub-delimiter char. Therefore it’s safe to use it to separate different parts in a URL; when it appears inside a part, it must be escaped as%21However, Java code dealing with JAR URL apparently follows the older RFC2396, in which
!was an unreserved char. It was unwise for Java to choose an unreserved char as separator. And when it did, it should at least internally escape it as if the char is reserved.