目录在Intellij中有效,但在CMD和.jar中无效



我在这部分代码中有问题。从文件夹中,iam加载所有图像,然后将其作为对象的一部分保存到这些对象的列表中。

当我在Intellij中运行它时,一切都很好。正是因为这个问题,我才打印isDirectory((。在intellij中,它返回true。

但当我尝试通过jar在cmd中运行时,就出现了问题。方法isDirectory((返回false,listFiles((抛出NullPointerException。

我还尝试手动将整个路径保存在String目录中,但没有任何更改。在intellij中,它运行得很好,在带有jar的CMD中也存在问题。问题就出在这里。在我开始使用这些函数之前,Jar工作得很好。谢谢你的意见。

private void loadAllExercises(){
public String directory = System.getProperty("user.dir") + "\pictures\";
File directory = new File(directory);
System.out.println(directory.isDirectory());
for (File file : directory.listFiles()) {
loadExercise(file);
}
}
private void loadExercise(File file){
if(file.getName().toLowerCase().endsWith(".jpg")) {
Exercise exercise = new Exercise(file.getName());
this.allExercises.add(exercise);
}
}

System.getProperty("user.dir")返回应用程序启动时的当前目录,可能不是您想要的。如果您想指向主目录,因为pictures目录就在那里,请尝试使用System.getProperty("user.home")

最新更新