Java Properties Load loadPropertiesFromResource(ClassLoader clsLoader, String resourcePath)

Here you can find the source of loadPropertiesFromResource(ClassLoader clsLoader, String resourcePath)

Description

Load properties from a resource embedded in the classpath.

License

Open Source License

Parameter

Parameter Description
clsLoader the ClassLoader to load the resource from
resourcePath the resource from which the properties should be loaded

Exception

Parameter Description
IllegalStateException if the properties cannot be loaded

Return

the Properties loaded from the resource

Declaration

public static Properties loadPropertiesFromResource(ClassLoader clsLoader, String resourcePath) 

Method Source Code

//package com.java2s;
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

import java.io.InputStream;

import java.net.URL;

import java.util.Properties;

public class Main {
    /**//from w ww . j  av a  2s  .c  om
     * Load properties from a resource embedded in the classpath.
     * Note that an IllegalStateException is thrown if the properties
     * cannot be loaded from the resource: therefore, this
     * method should probably only be used to load properties that are
     * guaranteed to be present.
     * 
     * @param clsLoader     the ClassLoader to load the resource from
     * @param resourcePath  the resource from which the properties should be loaded
     * @return the Properties loaded from the resource
     * @throws IllegalStateException if the properties cannot be loaded
     */
    public static Properties loadPropertiesFromResource(ClassLoader clsLoader, String resourcePath) {
        URL propURL = clsLoader.getResource(resourcePath);
        if (propURL == null) {
            throw new IllegalStateException("Couldn't find properties " + resourcePath);
        }
        Properties properties = new Properties();
        try {
            InputStream in = propURL.openStream();
            try {
                properties.load(in);
            } finally {
                in.close();
            }
        } catch (Exception e) {
            throw new IllegalStateException("Couldn't load properties " + resourcePath);
        }
        return properties;
    }
}

Related

  1. loadProperties(String properties)
  2. loadProperties(String propertyName)
  3. loadProperties(String resource)
  4. loadProperties(String resourceName)
  5. loadProperties(String resourceName, ClassLoader classLoader)
  6. loadPropertiesFromResource(String name)
  7. loadPropertiesFromResources(Class cls, String[] resources)
  8. loadPropertiesFromString(String propertiesString)
  9. loadPropertiesResources(String name)