java - 将外部文件夹添加到可运行 jar 中的 ClassPath
问题描述
我有一个 maven 项目,我使用 maven 程序集插件创建了一个带有依赖项的 jar。我还有一个外部配置文件( conf.properties ),jar 需要它才能正常工作。
我的项目结构是这样的:
|- abc.jar
|- config
|-conf.properties
如何将此配置文件夹添加到 jar 文件的类路径中?我尝试使用-cp
命令并在文件中操作类路径属性来做到这一点,MANIFEST.MF
但到目前为止还没有运气。
有谁知道这样做的方法?
解决方案
This is how I tested (sorry, no maven)!
Main class:
package cfh.sf.Chamika;
import java.util.ResourceBundle;
public class ABC {
public static void main(String[] args) {
var bundle = ResourceBundle.getBundle("conf");
System.out.println(bundle.getString("test"));
}
}
Manifest file, note empty line at end (entries must end with a newline (CR, LF or CRLF)):
Manifest-Version: 1.0
Main-Class: cfh.sf.Chamika.ABC
Class-Path: config/
Directory structure
dist/
abc.jar
config/
conf.properties
Content of conf.properties
:
test = OK, it is working!
Executed with
java -jar abc.jar
Alternative, not using ResourceBundle
:
package cfh.sf.Chamika;
import java.io.IOException;
public class ABC {
public static void main(String[] args) {
try (var input = ClassLoader.getSystemResourceAsStream("conf.properties")) {
int ch;
while ((ch = input.read()) != -1) {
System.out.print((char) ch);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
推荐阅读
- git - 如何恢复 GitHub 上的“合并拉取请求”选项并删除所有提交日志
- javascript - Javascript - 当我们点击 div 时添加样式
- python - 无法使用用户模型和信息模型导入模型名称 Django
- python - 在训练集上进行测试时,Keras 有状态 LSTM 的准确率很低
- elasticsearch - 在嵌套模型的弹性搜索中过滤查询
- ios - 以编程方式在 TabBarController 内创建 TableViewController 不会呈现标签
- java - 如何从另一个应用程序模块中的 VO 访问一个应用程序模块中的 VO?
- linux - 如何在 Linux 的汇编中找到程序进程 ID 号以自动终止程序?
- asciidoc - 让 asciidoc 生成嵌入乳胶的幻灯片
- google-apps-script - Google Analytics - 跟踪自定义维度以阻止它们被其他开发者移动