Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8187131

java.net.URI throws URISyntaxException when hostname contains underscore.

XMLWordPrintable

    • generic
    • generic

      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();
          }

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: