从JAR中的资源文件夹加载属性

时间:2012-02-19 12:15:01

标签: java file properties io inputstream

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

enter image description here

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

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

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

任何帮助都是值得的。提前致谢。

1 个答案:

答案 0 :(得分:4)

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

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