java - 如何迭代位于运行的 .jar 文件之外的文件夹
问题描述
我的 java 应用程序架构有问题。我想提供一个使用 Spring Boot 生成的简单 .jar 文件。在运行 .jar 之后,我需要遍历另一个文件夹中的文件。这是我的目录的层次结构:
- 我使用 .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");
推荐阅读
- ruby-on-rails - 无法为 ActiveModel 属性赋值(rails 4.2)
- python - 通过 curl 命令更新 json 文件
- go - 为什么我不能使用 flag.StringVar 传递指向 fmt.Println 的指针?
- asp.net-mvc - 添加 Xframeoptions 后未发生跨域通信:Sameorigin
- c# - 通过 http.get 将角度查询字符串作为 C# 类传递给 API 控制器
- java - Java中是否有所有接口的超级接口?
- algorithm - OpenCV 中的图像变换与点变换不匹配
- arrays - 将 PHP 数组传递给 NodeJS
- python - 在防火墙后面安装 Anaconda 包
- javascript - 用 Vuejs 读取本地 JSON 文件?