Tuesday, January 22, 2013

How to load resource from classpath in java

How to load resource from anywhere in classpath in java.

ClassLoader cl = ClassLoader.getSystemClassLoader();
    if (cl != null) {
        URL url = cl.getResource(CONF_PROPERTIES);
        if (null == url) {
            url = cl.getResource("/" + CONF_PROPERTIES);
        }
        if (null != url) {
            try {
                InputStream in = url.openStream();
                props = new Properties();
                props.load(in);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }


1 comment:

  1. In case we need only the resource path in the hard disk:
    String path = ClassLoader.getSystemClassLoader().getResource(".").getPath();

    ReplyDelete