JDK-8187131 : java.net.URI throws URISyntaxException when hostname contains underscore.
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 8,9
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2017-09-01
  • Updated: 2018-09-04
  • Resolved: 2017-09-04
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Linux john 4.4.0-93-generic #116-Ubuntu SMP Fri Aug 11 21:17:51 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
java.net.URI throws URISyntaxException when hostname contains underscore.

The following instruction will throw an URISyntaxException:

URI uriNotWorking = new URI("http", null,"my_site.xyz.com", 9999, "/hello/world", "myparam=ABCDE", null);

The following instruction will NOT throw an exception:

URI uriWorking = new URL("http://my_site.xyz.com:9999/hello/world?myparam=ABCDE").toUri();

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create an instance of java.net.URI such that the hostname contains an underscore:

URI uri = new URI("http", null,"my_site.xyz.com", 9999, "/hello/world", "myparam=ABCDE", null);

or

URI uri = new URI("http", null,"my_site.com", 9999, "/hello/world", "myparam=ABCDE", null);

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Instance of java.net.URI pointing to the correct URL.
ACTUAL -
Thrown instance of java.net.URISyntaxException.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.net.URISyntaxException: Illegal character in hostname at index 9: http://my_site.com:9999/hello/world?myparam=ABCDE
	at java.net.URI$Parser.fail(URI.java:2848)
	at java.net.URI$Parser.parseHostname(URI.java:3387)
	at java.net.URI$Parser.parseServer(URI.java:3236)
	at java.net.URI$Parser.parseAuthority(URI.java:3155)
	at java.net.URI$Parser.parseHierarchical(URI.java:3097)
	at java.net.URI$Parser.parse(URI.java:3053)
	at java.net.URI.<init>(URI.java:673)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public static void main(String... args) throws Exception {
        URI uri = new URI("http", null,"my_site.com", 9999, "/hello/world", "myparam=ABCDE", null);
    }
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
public static void main(String... args) throws Exception {
        URI uri = new URL("http://my_site.xyz.com:9999/hello/world?myparam=ABCDE").toUri();
    }


Comments
jshell> URI uriWorking = new URL("http://my_site.xyz.com:9999/hello/world?myparam=ABCDE").toURI(); uriWorking ==> http://my_site.xyz.com:9999/hello/world?myparam=ABCDE jshell> uriWorking.getHost(); $12 ==> null jshell> uriWorking.getPort(); $13 ==> -1 jshell> uriWorking.getAuthority(); $14 ==> "my_site.xyz.com:9999" jshell> uriWorking.parseServerAuthority(); | Exception java.net.URISyntaxException: Illegal character in hostname at index 9: http://my_site.xyz.com:9999/hello/world?myparam=ABCDE | at URI$Parser.fail (URI.java:2915) | at URI$Parser.parseHostname (URI.java:3450) | at URI$Parser.parseServer (URI.java:3299) | at URI$Parser.parseAuthority (URI.java:3218) | at URI$Parser.parseHierarchical (URI.java:3160) | at URI$Parser.parse (URI.java:3116) | at URI.parseServerAuthority (URI.java:945) | at (#15:1) jshell> uriWorking.toString(); $16 ==> "http://my_site.xyz.com:9999/hello/world?myparam=ABCDE"
04-09-2018

Closing this as duplicate of JDK-8180809.
04-09-2017