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

Tap*_*ose 2 java io properties inputstream file

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

在此处输入图片说明

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

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

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);
Run Code Online (Sandbox Code Playgroud)

但它显示:

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

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

file:/D:/Code/MovieLibrary/build/jar/Movie%20Library.jar!/resources/properties/movie_library.properties
Run Code Online (Sandbox Code Playgroud)

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

Jig*_*shi 6

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

props.load(LazyProperties.class.getResourceAsStream("/properties/movie_libraryproperties"));
Run Code Online (Sandbox Code Playgroud)

  • `LazyProperties.class.getResourceAsStream("classpath:/resources/properties/movie_library.p‌ roperties")` (2认同)