【问题标题】:Load Properties from resources folder within JAR从 JAR 中的资源文件夹加载属性
【发布时间】:2012-03-10 01:55:32
【问题描述】:

我有一个 JAR 文件 Movie Library.jar,其内容如下所示:

PropertiesUtils 类位于client.jar 中(如图所示),属性文件位于resources 文件夹中的properties 文件夹中。

我正在尝试将属性加载为:

String absolutePath = LazyProperties.class.getClass().getResource(filePath).getPath();
File file = new File(absolutePath);
InputStream stream = new FileInputStream(file);
Properties properties = new Properties();
properties.load(stream);

但它正在显示:

file:\D:\Code\MovieLibrary\build\jar\Movie%20Library.jar!\resources\properties\movie_library.properties (The filename, directory name, or volume label syntax is incorrect)

我无法将其视为System.out.println(file) 打印:

file:/D:/Code/MovieLibrary/build/jar/Movie%20Library.jar!/resources/properties/movie_library.properties

任何帮助都是可观的。提前致谢。

【问题讨论】:

    标签: java file properties io inputstream


    【解决方案1】:

    如果属性文件在jar文件中,则不再是物理文件

    props.load(LazyProperties.class.getResourceAsStream("/properties/movie_libraryproperties"));
    

    【讨论】:

    • 感谢@Jigar LazyProperties.class.getResourceAsStream("/resources/properties/movie_library.properties") 完成了这项工作。如果将资源文件夹放在client.jar中,您能告诉我语法是什么吗?
    • LazyProperties.class.getResourceAsStream("classpath:/resources/properties/movie_library.p‌​roperties")
    • @JigarJoshi :如果我想更改 jar 内的属性文件,有什么办法吗? ?
    猜你喜欢
    • 1970-01-01
    • 2018-06-03
    • 2017-06-28
    • 1970-01-01
    • 2014-10-11
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多