java.io.File #listFiles()从Windows cmd返回null,但不是Eclipse Project Run

时间:2021-07-31 02:29:42

I have created this following method which I execute from a main thread

我创建了以下方法,我从主线程执行

public static LinkedList<File> checkFile(String path, LinkedList<File> result) Throws IOException, SecurityException{
    File root = new File(path);
    File[] list = root.listFiles();

    if (list == null)
        return result;

    for (File f : list) {
        if (f.isDirectory()) {
            checkFile(f.getAbsolutePath(), result);
        } else {
                result.add(f.getAbsoluteFile());
        }
    }
    return result;
}

As long as it looks into the same location as the file itself, it can list all the files. But as soon as I point it to some other location (e.g. C:\somedir) it always returns null. When I run this inside eclipse, it is working fine. I don't assume that it's an issue with the classpath since I have added JAVA_HOME in windows path so it gets picked up (and the javac command also works fine).

只要它查找与文件本身相同的位置,它就可以列出所有文件。但是一旦我指向其他位置(例如C:\ somedir),它总是返回null。当我在日食中运行时,它工作正常。我不认为这是类路径的问题,因为我在Windows路径中添加了JAVA_HOME,因此它被拾取(并且javac命令也可以正常工作)。

I remember that there was a java bug in version 1.4 which was based around the error handling of listFiles method execution. But I am not sure why this is not working when I run it from command line using java command.

我记得版本1.4中有一个java bug,它基于listFiles方法执行的错误处理。但是当我使用java命令从命令行运行它时,我不确定为什么这不起作用。

you can use the following wrapper to run it

您可以使用以下包装器来运行它

public static void main(String[] args) throws IOException {

    if (args.length == 0 || args[0] == null){
        System.out.println("please specify path to the project's work folder");
        return;
    }

    String folderPath = (String) args[0];
    LinkedList<File> result = new LinkedList<>();

    result = checkFile(folderPath, result);
    long startTime = System.currentTimeMillis();
    for (File file : result) {
        System.out.println(file.getName());
    }

}

EDIT

Okay, I have realised that I used a literal "path" instead of the parameter path and thanks for everyone who pointed this out. But the main problem prevails. The path is not recognised if it is not in the location as the executable class itself. When I try to run this from Eclipse, as a project, it works! But when I try to do it from command prompt using simple command, It doesn't work and #listFiles() always returns null.

好吧,我已经意识到我使用了文字“路径”而不是参数路径,并感谢所有指出这一点的人。但主要问题占上风。如果路径不在可执行类本身的位置,则无法识别该路径。当我尝试从Eclipse运行它时,作为一个项目,它的工作原理!但是当我尝试使用简单命令从命令提示符执行此操作时,它不起作用,#listFiles()始终返回null。

java SomeExecItem -C:\myFolder\withclasses

java SomeExecItem -C:\ myFolder \ withclasses

From java.io.File#listFiles() method source analysis, What I understand is that if isInvalid() is evaluated as false when I run it from command line. The only possible reason is that the indexOf('\u0000') is evaluated as -1?

从java.io.File#listFiles()方法源码分析,我的理解是,如果isInvalid()从命令行运行时被评估为false。唯一可能的原因是indexOf('\ u0000')被评估为-1?

What is the difference here? Since I am not running in admin mode, I suppose Eclipse's view/read permission should be the same as a standalone command line application in cmd prompt?

这有什么区别?由于我没有在管理模式下运行,我认为Eclipse的查看/读取权限应该与cmd提示符中的独立命令行应用程序相同?

2 个解决方案

#1


0  

You are not passing the parameter correctly. The try java SomeExecItem "C:\myFolder\withclasses" or java SomeExecItem C:\myFolder\withclasses.

您没有正确传递参数。试试java SomeExecItem“C:\ myFolder \ withclasses”或java SomeExecItem C:\ myFolder \ withclasses。

#2


2  

Change File root = new File("path"); to File root = new File(path);. This will fix the problem.

更改文件root = new File(“path”); to file root = new File(path);.这将解决问题。

#1


0  

You are not passing the parameter correctly. The try java SomeExecItem "C:\myFolder\withclasses" or java SomeExecItem C:\myFolder\withclasses.

您没有正确传递参数。试试java SomeExecItem“C:\ myFolder \ withclasses”或java SomeExecItem C:\ myFolder \ withclasses。

#2


2  

Change File root = new File("path"); to File root = new File(path);. This will fix the problem.

更改文件root = new File(“path”); to file root = new File(path);.这将解决问题。