title: Java URL Class getPath(), getQuery() and getFile() inconsistent with RFC3986 URI Syntax
body: <p>I am writing a utility class that semi-wraps Java's <code>URL class</code>, and I have written a bunch of test cases to verify the methods I have wrapped with a customized implementation. I don't understand the output of some of Java's getters for certain <code>URL</code> strings.</p>
  
  <p>According to the RFC 3986 specification, a path component is defined as follows:</p>
  
  <pre><code>The path is terminated by the first question mark ("?") or number sign   
  ("#") character, or by the end of the URI.
  </code></pre>
  
  <p>A query component is defined as follows:</p>
  
  <pre><code>The query component is indicated by the first question
  mark ("?") character and terminated by a number sign ("#") character
  or by the end of the URI.
  </code></pre>
  
  <p>I have a couple test cases which are treated by Java as valid URLs, but getters for path, file and query don't return the values I had expected:</p>
  
  <pre><code>URL url = new URL("https://www.somesite.com/?param1=val1");
  
  System.out.print(url.getPath());
  System.out.println(url.getFile());
  System.out.println(url.getQuery());
  </code></pre>
  
  <p>The above results in the following output:</p>
  
  <pre><code>//?param1=val1
  param1=val1
  &lt;empty string&gt;
  </code></pre>
  
  <p>My other test case:</p>
  
  <pre><code>URL url = new URL("https://www.somesite.com?param1=val1");
  
  System.out.print(url.getPath());
  System.out.println(url.getFile());
  System.out.println(url.getQuery());
  </code></pre>
  
  <p>The above results in the following output:</p>
  
  <pre><code>?param1=val1
  param1=val1
  &lt;empty string&gt;
  </code></pre>
  
  <p>According to the documentation for Java <code>URL</code>:</p>
  
  <pre><code>public String getFile()
  
  Gets the file name of this URL. The returned file portion will be the  
  same as getPath(), plus the concatenation of the value of getQuery(), if 
  any. If there is no query portion, this method and getPath() will return 
  identical results.
  
  Returns:
      the file name of this URL, or an empty string if one does not exist
  </code></pre>
  
  <p>So, my test cases result in empty string when <code>getQuery()</code> is invoked. In which case, I would expected <code>getFile()</code> to return the same value as <code>getPath()</code>. This is not the case.</p>
  
  <p>I had expected the following output for both test cases:</p>
  
  <pre><code>&lt;empty string&gt;
  ?param1=val1
  param1=val1
  </code></pre>
  
  <p>Maybe my interpretation of the RFC 3986 is not correct. But the output I have seen also does not line up with the documentation for the URL class either? Can anyone explain what I am seeing?</p>