Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
480 views
in Technique[技术] by (71.8m points)

java - Load Properties from resources folder within JAR

I have a JAR file Movie Library.jar, its contents are depicted below:

enter image description here

The class PropertiesUtils is resides in the client.jar (shown in picture) and the properties files are in properties folder which resides in resources folder.

I am trying to load properties as:

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);

But it is showing:

file:D:CodeMovieLibraryuildjarMovie%20Library.jar!
esourcespropertiesmovie_library.properties (The filename, directory name, or volume label syntax is incorrect)

I am unable to figure it as System.out.println(file) prints:

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

Any help is appreciable. Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If the property file is inside jar file, it is no longer a physical file

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...