Java Collection How to - Load a properties file in the classpath








Question

We would like to know how to load a properties file in the classpath.

Answer

  //from w  ww .  j  a  v  a2  s  . com


import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.Properties;

public class Main {

  public static void main(String args[]) throws Exception {
    
    Properties props = new Properties();
    URL url = ClassLoader.getSystemResource("myprops.props");
    props.load(url.openStream());
    System.out.println(props);

  }
}

The code above generates the following result.