简体   繁体   English

线程“ main”中的异常java.nio.file.InvalidPathException:UNC路径缺少主机名:/ \\ /

[英]Exception in thread “main” java.nio.file.InvalidPathException: UNC path is missing hostname: /\/

I am trying to figure out a way to solve my problem. 我正在尝试找出解决问题的方法。 I am using java.nio . 我正在使用java.nio When i execute Paths.get("/","/").toString() in Linux environment, it is working fine as it is Linux based path. 当我在Linux环境中执行Paths.get("/","/").toString()时,它工作正常,因为它是基于Linux的路径。 But when i execute it in Windows environment, it gives me the following error. 但是,当我在Windows环境中执行它时,它给了我以下错误。

Exception in thread "main" java.nio.file.InvalidPathException: UNC path is missing hostname: /\/
        at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:113)
        at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
        at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
        at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
        at java.nio.file.Paths.get(Paths.java:84)

I understand this is not a valid path system in Windows. 我了解这不是Windows中有效的路径系统。 Is there any way so that i can work for both Windows and Linux? 有什么办法可以使我同时在Windows和Linux上工作?

Note: There are lot of hard coded Forward Slash in our application. 注意:我们的应用程序中有很多硬编码的正斜杠。

Paths.get("/","/") is trivially useless, so I've no idea what your real use case is. Paths.get("/","/")没有用,所以我不知道您真正的用例是什么。 However, you should never need to use hard-coded file separator in your code. 但是,您永远不需要在代码中使用硬编码的文件分隔符。

Assuming you want to get the root directory of a file system, you could do 2 things: 假设您要获取文件系统的根目录,则可以做两件事:

  1. Paths.get(".").getRoot() would return / if $PWD=/home/blah 如果$PWD=/home/blahPaths.get(".").getRoot()将返回/
  2. FileSystems.getDefault().getRootDirectories()

If you don't need the root directory, Paths.get would construct a Path using File.separator . 如果不需要根目录,则Paths.get将使用File.separator构造一个Path

Only the first parameter should be absolute, ie start with a path separator ( / or \\ ). 只有第一个参数应该是绝对的,即以路径分隔符( /\\ )开头。

If the second value can be absolute, ie you want to ignore the preceding path, use: 如果第二个值可以是绝对值,即要忽略前面的路径,请使用:

Paths.get("/").resolve("/").toString()   // returns "\" on Windows

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“main”中的异常 java.nio.file.InvalidPathException:索引 72 处的非法字符 <:> - Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 72 java.nio.file.InvalidPathException - java.nio.file.InvalidPathException java.nio.file.InvalidPathException 当路径包含冒号 (:) - java.nio.file.InvalidPathException when the path contains colon (:) 带有getPath的java.nio.file.InvalidPathException - java.nio.file.InvalidPathException with getPath 系统错误:java.nio.file.InvalidPathException: - SystemError: java.nio.file.InvalidPathException: 从 src/main/resources 读取会在命令行上给出 java.nio.file.InvalidPathException (但在 Eclipse 中工作正常) - Reading from src/main/resources gives java.nio.file.InvalidPathException on command line (but works fine in Eclipse) gradle-resources-production:mymod.main: java.nio.file.InvalidPathException: Illegal char &lt;:&gt; at index 75 - gradle-resources-production:mymod.main: java.nio.file.InvalidPathException: Illegal char <:> at index 75 为什么这会抛出 java.nio.file.InvalidPathException? - Why does this throw java.nio.file.InvalidPathException? 遇到java.nio.file.InvalidPathException:非法char &lt;:&gt; - Encountering java.nio.file.InvalidPathException: Illegal char <:> java.nio.file.InvalidPathException:索引 2 处的非法字符 <:>: - java.nio.file.InvalidPathException: Illegal char <:> at index 2:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM