Java Collection How to - Load a Properties file stored in a JAR








Question

We would like to know how to load a Properties file stored in a JAR.

Answer

  //from ww  w.  j a v a  2  s .c  o  m

import java.net.URL;
import java.util.Properties;

import javax.swing.JApplet;

public class Main extends JApplet {
  public static void main(String[] a) throws Exception {
    Properties p = new Properties();
    URL url = ClassLoader.getSystemResource("/com/java2s/config/system.props");
    if (url != null)
      p.load(url.openStream());

  }
}