在Intellij中有效,在CMD和.jar中无效。

huangapple go评论70阅读模式
英文:

Directory is valid in Intellij but NOT in CMD and .jar

问题

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

当我在Intellij中运行它时,一切正常。我打印isDirectory()只是因为这个问题。在Intellij中,它返回true。

但是当我尝试在命令行中通过jar运行它时,就出问题了。isDirectory()方法返回false,并且listFiles()抛出NullPointerException。

我还尝试手动将整个路径保存在字符串目录中,但没有改变。在Intellij中运行得非常完美,问题出在使用这些函数的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);
    }
}
英文:

i have problem in this part of code. From folder, iam loading all images and then save it as part of object to list of these object.

When i run it in Intellij, everything is OK. I print isDirectory() just because of this problem. In intellij it returns true.

But when i try it to run in cmd through jar, than there is problem. Method isDirectory() returns false and listFiles() throws NullPointerException.

I also tried to save in String directory whole path manually, but nothing changed. In intellij it runs perfectly and its problem in CMD with jar. Problem is really in this. Jar worked perfectly before i started use these functions. Thank you for any ideas.

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

答案1

得分: 1

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

英文:

System.getProperty("user.dir") returns the current directory when the application was started and is probably not what you intended. If you want to point to your home directory because that's where your pictures directory is, try using System.getProperty("user.home").

huangapple
  • 本文由 发表于 2020年5月5日 06:47:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/61602913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定