一段测试程序是这样的,目的是读取某个properties文件里面的key的值来初始化一个class。帮忙看看啊
public class ClassLoaderTest {
public static void main(String[] args) {
try {
Enumeration enum = ClassLoaderTest.class.getClassLoader()
.getResources("C:/../repository.properties");
if (enum.hasMoreElements()) {
URL fileUrl = (URL) enum.nextElement();
Properties repository = new Properties();
repository.load(fileUrl.openStream());
String className = (String)repository.getProperty("key");
Class clazz = Class.forName(className);
Object obj = clazz.newInstance();
}catch(){
}....
}为什么不行呢?好像是读不到那个文件,我想知道的重点是能不能用ClassLoaderTest.class.getClassLoader(),也就是说用ClassLoaderTest的classLoader来读那些资源文件?是不是任何一个已经定义了的class的classLoader都可以用来读这些资源文件呢?若不是,有些什么规则啊?
盼指点。
public class ClassLoaderTest {
public static void main(String[] args) {
try {
Enumeration enum = ClassLoaderTest.class.getClassLoader()
.getResources("C:/../repository.properties");
if (enum.hasMoreElements()) {
URL fileUrl = (URL) enum.nextElement();
Properties repository = new Properties();
repository.load(fileUrl.openStream());
String className = (String)repository.getProperty("key");
Class clazz = Class.forName(className);
Object obj = clazz.newInstance();
}catch(){
}....
}为什么不行呢?好像是读不到那个文件,我想知道的重点是能不能用ClassLoaderTest.class.getClassLoader(),也就是说用ClassLoaderTest的classLoader来读那些资源文件?是不是任何一个已经定义了的class的classLoader都可以用来读这些资源文件呢?若不是,有些什么规则啊?
盼指点。
(this actually means you should put the file in your classpath because you are using ClassLoader)
2.ClassLoaderTest.class.getClassLoader().getResources("repository.properties");