2

I have looked at the solution provided in this question What does 'URI has an authority component' mean?

before asking this question, but I am having the same issue in Intellij. What I have is

  1. maven project
  2. somefile.txt is in resource folder and there is no typo in the name

Source code:

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;

public class FileLoading {
    public static void main(String ... args){
        File someFile = null;
        try {
            someFile = new File( new URI("file://somefile.txt"));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        System.out.println(someFile.getPath());
    }
}

Error:

"C:\Program Files\Java\jdk1.8.0_162\bin\java" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2017.3.4\lib\idea_rt.jar=51711:C:\Program Files\JetBrains\IntelliJ IDEA 2017.3.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_162\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\rt.jar;C:\Users\U631156\code\java\target\classes" FileLoading
Exception in thread "main" java.lang.IllegalArgumentException: URI has an authority component
    at java.io.File.<init>(File.java:423)
    at FileLoading.main(FileLoading.java:9)

Process finished with exit code 1

How do I resolve this.

Ashish
  • 14,295
  • 21
  • 82
  • 127
  • 1
    Have you tried `file:///`? The authority component begins with `//`; it's followed by the path, which starts with `/`. – erickson May 30 '18 at 18:54

1 Answers1

4

If you're not specifying a host for the file, the syntax should be file:///somefile.txt (note 3 slashes).

The relevant RFC RFC 8089 shows the structure of the URI in section 2, which identifies the part after 2 slashes as the auth-path (hence your error).

Note, however, that this will put "somefile.txt" in the root of your filesystem, since the URI has no directory path.

Community
  • 1
  • 1
DavidW
  • 1,413
  • 10
  • 17
  • What do you mean by host for the file ? – Ashish May 30 '18 at 19:01
  • @ashish The machine/node/resolvable location where the file resides. For example, `file://downloads.example.org:2049/public/patches/ver-1.0-2.1.patch` would refer to a file hosted on an Internet accessible server at `downloads.example.org`, served by NFS, and with a local file path (relative to the root the service runs in) of `/public/patches/ver-1.0-2.1.patch`. – DavidW May 30 '18 at 19:53
  • So how can relative URI be written? – Roy Ash Jul 13 '20 at 22:42
  • @RoyAsh What do you mean by"relative" URI? Perhaps you should ask this as a new question. – DavidW Jul 13 '20 at 23:03
  • I'll give an example: I want to do `spring.data.neo4j.uri=file:///~/src/main/BlockchainDB` – Roy Ash Jul 13 '20 at 23:04
  • And the JVM gives me the error `java.nio.file.FileSystemException: /~: Read-only file system` What is the problem here? – Roy Ash Jul 13 '20 at 23:05
  • Even assuming Spring will do shell expansions, you're asking for `/~/src/main/BlockchainDB` and there's no file named "~" in /. Put in the full expanded path to the file. – DavidW Jul 13 '20 at 23:20