Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
486 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...