java.io.File #listFiles()从Windows cmd返回null,但不作为Eclipse Project Run返回 [英] java.io.File #listFiles() Returns null from Windows cmd, but not as Eclipse Project Run

查看:129
本文介绍了java.io.File #listFiles()从Windows cmd返回null,但不作为Eclipse Project Run返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了以下方法,该方法是从main线程执行的

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;
}

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

我记得 1.4版中有一个Java错误,它基于对listFiles方法执行的错误处理.但是我不确定为什么当我使用java命令从命令行运行它时,它不能正常工作.

您可以使用以下包装程序运行它

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());
    }

}

编辑

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

java SomeExecItem -C:\myFolder\withclasses

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

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

解决方案

您没有正确传递参数.尝试java SomeExecItem "C:\myFolder\withclasses"java SomeExecItem C:\myFolder\withclasses.

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).

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.

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.

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?

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?

解决方案

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

这篇关于java.io.File #listFiles()从Windows cmd返回null,但不作为Eclipse Project Run返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆