File.exists() always returns false when using Map Network Drive for a shared folder

239 views Asked by At

On my Windows Server (2012 R2), I added a shared folder from another server to a Mapped Network Drive (X:) as in the image below.

enter image description here

Then, I have this snipper code to get files from a specified directory.

public void getFiles(String fileLocation) throws Exception {
    File file = new File(fileLocation);
    if (file.exists()) {
        if (file.isDirectory()) {                
            //get files from dir
        } else {
            //get file
        }
    } else {
        logger.info("Not found directory: " + fileLocation);
    }
}

Now, the weird issue is, on this Windows Server (2012 R2), when I pass X:\ as the input parameter for the getFiles() method, file.exist() is then always false.

However, If I run the same snipper code with the same input value on other servers (test on another Windows Server 2012 R2, or Windows 10 Pro - my PC), it returns true as I expected.

All those machines are joined to the same domain, and of course, they add the same shared resource to the same Map Network Drive (X:)

Does anyone know what could expose this issue?

UPDATE

I changed the code to use JAVA 8 API Files, but still got the same issue on that Windows 2012 R2 server.

Here is the code running on the server.

public void upload(String fileLocation) throws Exception {
    Path filePath = Paths.get(fileLocation);
    if (Files.exists(filePath)) {
        if (Files.isDirectory(filePath)) {
            //get and process multiple files

        } else {
            //process a single file
        }
    } else {
        logger.info("Not found directory '" + fileLocation + "'. Skip uploading files!");
    }
}

Debug log of the upload() method

enter image description here

Then, I create a sample console app to test the same logic code on the same server, and it works normally. The sample console code.

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;

public class Main {
    public static void main(String[] args) {
        String fileLocation = "Y:\\";

        try {
            File file = new File(fileLocation);
            Files.readAttributes(file.toPath(), BasicFileAttributes.class);

            if (Files.exists(Paths.get(fileLocation))) {
                System.out.println("Found: " + fileLocation);

                if (Files.isDirectory(Paths.get(fileLocation))) {
                    System.out.println(fileLocation + " is a directory");
                } else {
                    System.out.println(fileLocation + " is a file");
                }

            } else {
                System.out.println("Not Found: " + fileLocation);
            }

        } catch (Exception e) {
            System.out.println("Error while checking file location: " + fileLocation);
            e.printStackTrace();
        }
    }
}

enter image description here

0

There are 0 answers