Here you can find the source of loadPropertiesFromResource(ClassLoader clsLoader, String resourcePath)
Parameter | Description |
---|---|
clsLoader | the ClassLoader to load the resource from |
resourcePath | the resource from which the properties should be loaded |
Parameter | Description |
---|---|
IllegalStateException | if the properties cannot be loaded |
public static Properties loadPropertiesFromResource(ClassLoader clsLoader, String resourcePath)
//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; } }