Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Special URLs characters

Tags:

java

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.

like image 376
Nolesh Avatar asked Jan 10 '12 06:01

Nolesh


People also ask

Can URL pass special characters?

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.

What characters are forbidden in URL?

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.


1 Answers

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.

like image 93
irreputable Avatar answered Sep 21 '22 19:09

irreputable