Question

Following leads in this thread and others, I've set up a code block where I map a network drive to a local drive and then I list some files inside it.

The problem is: it looks like the mapping is not available yet to the FileUtils method in the first call and, only if I wait and call the routine again, it finds the mapped drive ready to be used.

String path = "B:/Files/Somwething/SomethingElse/FinalDepth";
File newFile = new File("B:/Files/Something/SomethingElse");
if (!newFile.isDirectory()) {
    flagNetwork = true;
    p = Runtime.getRuntime().exec("net use B: " + networkSharedFolder);

    if(p != null) {
        Collection<File> files = FileUtils.listFiles(new File(path), arrExt, false);
        Iterator<File> iterFiles = files.iterator();
        while(iterFiles.hasNext()) {
            File tmpFile = (File) iterFiles.next();
            listResult.add(tmpFile);
        }
        LogServicio.doLog("[MyApplication, ContextListener] Drive B mapped to " + networkSharedFolder + ".", LogServicio.CErrorLvl);
    } else {
        throw new Exception("Error mapping network drive.");
    }
}

¿What should I do in order to use the mapped drive at once (or to guarantee that I waited until the drive is available)?

Was it helpful?

Solution

Runtime.exec starts a process running, but doesn't really wait for it. You'd have to wait for the process to finish (via something like p.waitFor()) before you know the drive's mapped.

...
if (p != null) {
    while (true) {
        try { p.waitFor(); break; }
        catch (InterruptedException ex) { /* don't care */ }
    }

    Collection<File> files ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top