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

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

public static LinkedList checkFile(String path, LinkedList 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 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值