Reputation: 83
I have a situation to access a shared folders. Following is a sample java program.
import java.nio.file.*;
/**
* Test
*/
public class Test
{
public static void main(String[] args)
{
String strPath = "//WG0202";
Path path = FileSystems.getDefault().getPath(strPath).getRoot();
if (path != null)
{
System.out.println(path.toFile().exists());
}
}
}
Let us assume as below -
Computer name: WG0202
A folder shared in this computer is: TestFolder
So if I give the path as: //WG0202/TetFolder
Then it works fine.
But if I give the path as: //WG0202
Then it is failing with the below exception -
Exception in thread "main" java.nio.file.InvalidPathException: UNC path is missing sharename: //WG0202 at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:118) 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)
Upvotes: 2
Views: 15513
Reputation: 399
An UNC path is indeed made out of a host and a share. You are trying to use UNC without a share, as your exception states.
To clarify:
//WG0202/TestFolder
is a valid UNC path:
//WG0202/
is not.
Cheers
Upvotes: 4
Reputation: 5762
From Official Javadoc of File
User interfaces and operating systems use system-dependent pathname strings to name files and directories. This class presents an abstract, system-independent view of hierarchical pathnames. An abstract pathname has two components:
An optional system-dependent prefix string, such as a disk-drive specifier,
"/"
for the UNIX root directory, or"\\\\"
for a Microsoft Windows UNC pathname, andA sequence of zero or more string names.
The first name in an abstract pathname may be a directory name or, in the case of Microsoft Windows UNC pathnames, a hostname. Each subsequent name in an abstract pathname denotes a directory; the last name may denote either a directory or a file. The empty abstract pathname has no prefix and an empty name sequence.
Upvotes: 1