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.
For instance, the "#" character needs to be encoded because it has a special meaning of that of an html anchor. The <space> character needs to be encoded because it is not a valid URL character. Also, some characters, such as "~" might not transport properly across the internet.
URLs and SEO Forbidden URL characters List of additional characters that cannot be used in URLs (page aliases and URL paths). The following characters are forbidden by default: \/:*? "<>|&%. '#[]+=„“ and the space character.
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!!!/
into Ares!!%21/
, but keep myfile.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 %21
However, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With