【问题标题】:How to iterate on a folder located outside a .jar file running如何迭代位于运行的 .jar 文件之外的文件夹
【发布时间】:2020-07-14 20:38:44
【问题描述】:

我的 java 应用架构有问题。我想提供一个使用 Spring Boot 生成的简单 .jar 文件。在运行 .jar 之后,我需要遍历另一个文件夹中的文件。 这是我的目录的层次结构:

enter image description here

  1. 我使用 .bat 或 sh 启动我的 .jar,使用 java -jar myApp.jar
  2. 我必须迭代 myApp/files 中存在的所有文件

这看起来很简单,但我找不到办法做到这一点!我尝试了很多在这里找到的东西,但没有任何效果!

我试试这个,但有一个 nullPointer...

File jarFile = new File(myMainClass.class.getProtectionDomain().getCodeSource().getLocation().toURI());
        String jarLocation = jarFile.getPath();

你能帮我做一些尝试吗!

谢谢!

【问题讨论】:

  • 找出何时收到 NullPointerException。

标签: java spring-boot file jar


【解决方案1】:

编辑你的 .bat 文件,像这样进入你的 myApp 文件夹

@echo off

#do stuff

cd %myApp_folder%

java -jar myapp.jar

您可以通过这样的相对路径解析您的文件夹:

File confFolder = new File("conf");
File filesFolder = new File("files");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 2023-04-10
    • 1970-01-01
    • 2017-04-23
    • 2019-05-21
    • 2018-01-10
    相关资源
    最近更新 更多