Load Properties from resources folder within JAR

11,744

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

props.load(LazyProperties.class.getResourceAsStream("/properties/movie_libraryproperties"));
Share:
11,744
Tapas Bose
Author by

Tapas Bose

Java developer.

Updated on June 05, 2022

Comments

  • Tapas Bose
    Tapas Bose almost 2 years

    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:\Code\MovieLibrary\build\jar\Movie%20Library.jar!\resources\properties\movie_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.

  • Tapas Bose
    Tapas Bose about 12 years
    Thanks @Jigar LazyProperties.class.getResourceAsStream("/resources/propert‌​ies/movie_library.pr‌​operties") did the job. Can you tell me what is the syntax if the resources folder be placed in client.jar?
  • jmj
    jmj about 12 years
    LazyProperties.class.getResourceAsStream("classpath:/resourc‌​es/properties/movie_‌​library.p‌​roperties‌​")
  • Harshil
    Harshil about 8 years
    @JigarJoshi : Is there any way if I want to do changes in the property file inside the jar. ?