Search code examples
javaurijava.nio.file

IllegalArgumentException: URI is not hierarchical when calling existing folder


I'm doing this:

private boolean createCopy(String targetDirectory, String[] dataSet, String fileName, boolean overwrite) throws IOException, URISyntaxException
{
    fileName = "file:" + fileName.replace(" ","%20");
    URI uri = new URI("file:" + targetDirectory);
    Path dPath = Paths.get(uri);
    //code
 }

And I get this exception:

Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierarchical
at sun.nio.fs.WindowsUriSupport.fromUri(WindowsUriSupport.java:122)
at sun.nio.fs.WindowsFileSystemProvider.getPath(WindowsFileSystemProvider.java:92)
at java.nio.file.Paths.get(Paths.java:138)
...

At the

Path dPath = Paths.get(uri);

line. Anyone having any idea why that is? targetDirectory is just a simple folder, neither a JAR nor a WAR file ; if I do away with the

URI uri = new URI("file:" + targetDirectory);

I just get

Exception in thread "main" java.nio.file.FileSystemNotFoundException: Provider "DRIVE_LETTER" not installed
at java.nio.file.Paths.get(Paths.java:147)
...

"DRIVE_LETTER" ends up being something like "C" or "D" or "E". It's the drive that targetDirectoryis located on.

EDIT:

public static void main(String... args)
{
    Path path = null;
    try
    {
        Paths.get(new URI("file:E://HTML%20Processor//test//copies//"));
    }
    catch (URISyntaxException e)
    {
        e.printStackTrace();
    }
}

Throws exactly the same exceptions, since you are wondering about the exact call.

EDIT: Putting the file on any other drive makes no difference; USB or SATA drive makes no difference either.


Solution

  • it should be file:// not file: