-3

JDK20 URL Constructors deprecated

Deprecated URL constructor: public URL(URL context, String spec) throws MalformedURLException

The following attempts have been unsuccessful on CentOS 7:

  • Original Working Solution (with deprecated URL constructor mentioned above):
    • Deprecated API: url = new URL(context,spec);
    • Results with deprecated code:
      • Case 1:

        Context: file:jar:http://myserver/my/jars/my-jar.jar!/mydir/mydir/test_hello.txt, spec=abc.txt
        Resulting URL: file:jar:http://myserver/my/jars/my-jar.jar!/mydir/mydir/abc.txt
        
      • Case 2:

        Context: file:ram:my_textfile_original, spec: my.dir.input_test.txt
        Resulting URL: file:my.dir.input_test.txt
        
  • Unsuccessful Attempt 1:
    • New Code: newURL = new File(context).toURI().resolve(spec).toURL();
    • Results with new code:
      • Case 1:

        Context: file:jar:http://myserver/my/jars/my-jar.jar!/mydir/mydir/test_hello.txt, spec=abc.txt
        Resulting URL: file:/home/user/my/homedir/otherdir/file:jar:http:/myserver/my/jars/my-jar.jar!/mydir/mydir/abc.txt
        
      • Case 2:

        Context: file:ram:my_textfile_original, spec: my.dir.input_test.txt
        Resulting URL: file:/home/user/my/homedir/otherdir/my.dir.input_test.txt
        
  • Unsuccessful Attempt 2:
    • New Code:

      Path myPath = Paths.get(context,spec);
      URI newURI = new URL(myPath.toString());
      url = newURI.toURL();
      
    • Results with new code:

      • Case 1:

        Context: file:jar:http://myserver/my/jars/my-jar.jar!/mydir/mydir/test_hello.txt, spec=abc.txt
        Resulting URL: file:jar:http:/myserver/my/jars/my-jar.jar!/mydir/mydir/abc.txt
        
      • Case 2:

        Context: file:ram:my_textfile_original, spec: my.dir.input_test.txt
        Resulting URL: file:ram:my_textfile_original/my.dir.input_test.txt
        
  • 2
    Related: [*How to replace the deprecated URL constructors in Java 20?*](https://stackoverflow.com/q/75966165/642706) – Basil Bourque Aug 25 '23 at 14:04
  • URI.resolve is the correct way to do it. But first, you need to have a valid context URI. Your context URIs are not valid. A file: URI just the letters `file:` followed by a path (using forward slashes, on every platform), not another scheme. – VGR Aug 25 '23 at 14:07
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 25 '23 at 20:32

0 Answers0