creating a virtual file system with JIMFS

The problem is a mix of Default FileSystem and new FileSystem.

Problem 1:

Files.createDirectory("/virtualfolder"); 

This will actually not compile so I suspect you meant:

Files.createDirectory( Paths.get("/virtualfolder"));

This attempts to create a directory in your root directory of the default filesystem. You need privileges to do that and probably should not do it as a test. I suspect you tried to work around this problem by using strings and run into

Problem 2:

Lets look at your code with comments

FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
// now get path in the new FileSystem
Path data = fs.getPath("/virtual");    
// create a directory in the new FileSystem
Path dir = Files.createDirectory(data);
// create a file in the default FileSystem 
// with a parent that was never created there
Path file = Files.createFile(Paths.get(dir + "/abc.txt")); // throws NoSuchFileException

Lets look at the last line:

dir + "/abc.txt"            >> is the string "/virtual/abc.txt"
Paths.get(dir + "/abc.txt") >> is this as path in the default filesystem

Remember the virtual filesystem lives parallel to the default filesystem. Paths have a filesystem and can not be used in an other filesystem. They are not just names.

Notes:

  • Working with virtual filesystems avoid the Paths class. This class will always work in the default filesystem. Files is ok because you have create a path in the correct filesystem first.

  • if your original plan was to work with a virtual filesystem mounted to the default filesystem you need bit more. I have a project where I create a Webdav server based on a virtual filesystem and then use OS build in methods to mount that as a volume.