如何迭代位于 .jar 文件之外的文件夹 运行
How to iterate on a folder located outside a .jar file running
我的 java 应用架构有问题。我想提供一个使用 Spring Boot 生成的简单 .jar 文件。我需要遍历另一个文件夹中的文件,在 运行 .jar 之后。
这是我的目录的层次结构:
enter image description here
- 我使用 .bat 或 sh 启动我的 .jar,使用 java -jar myApp.jar
- 我必须迭代 myApp/files
中存在的所有文件
这看起来很简单,但我找不到办法做到这一点!我尝试了很多在这里找到的东西,但没有任何效果!
我尝试了这个但是有一个 nullPointer...
File jarFile = new File(myMainClass.class.getProtectionDomain().getCodeSource().getLocation().toURI());
String jarLocation = jarFile.getPath();
你能帮我做一些尝试吗!
谢谢!
像这样编辑您的 .bat 文件以启动到您的 myApp 文件夹
@echo off
#do stuff
cd %myApp_folder%
java -jar myapp.jar
您可以像这样通过相对路径解析您的文件夹:
File confFolder = new File("conf");
File filesFolder = new File("files");
我的 java 应用架构有问题。我想提供一个使用 Spring Boot 生成的简单 .jar 文件。我需要遍历另一个文件夹中的文件,在 运行 .jar 之后。 这是我的目录的层次结构:
enter image description here
- 我使用 .bat 或 sh 启动我的 .jar,使用 java -jar myApp.jar
- 我必须迭代 myApp/files 中存在的所有文件
这看起来很简单,但我找不到办法做到这一点!我尝试了很多在这里找到的东西,但没有任何效果!
我尝试了这个但是有一个 nullPointer...
File jarFile = new File(myMainClass.class.getProtectionDomain().getCodeSource().getLocation().toURI());
String jarLocation = jarFile.getPath();
你能帮我做一些尝试吗!
谢谢!
像这样编辑您的 .bat 文件以启动到您的 myApp 文件夹
@echo off
#do stuff
cd %myApp_folder%
java -jar myapp.jar
您可以像这样通过相对路径解析您的文件夹:
File confFolder = new File("conf");
File filesFolder = new File("files");