2013-02-21 135 views
1

我想在运行时将/home/nameuser/.myProject/conf下的配置文件添加到类路径中。动态修改类路径

我试过用URLClassLoader但没有成功。

URL[] url={new URL("file://foo")}; 
URLClassLoader loader = new URLClassLoader(url); 

ClassLoader currentThreadClassLoader 
= Thread.currentThread().getContextClassLoader(); 

// Add the conf dir to the classpath 
// Chain the current thread classloader 
URLClassLoader urlClassLoader 
= new URLClassLoader(new URL[]{new File("mtFile").toURL()}, 
         currentThreadClassLoader); 

// Replace the thread classloader - assumes 
// you have permissions to do so 
Thread.currentThread().setContextClassLoader(urlClassLoader); 
+0

用classloader [负载](HTTP:/ /docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#loadClass%28java.lang.String%29)目标类并使用反射来利用它。 – McDowell 2013-02-21 16:53:38

+0

我的文件不是类但配置文件 – Toisur 2013-02-21 17:06:51

+1

你的代码中有以下内容:'URL [] url = {new URL(“file:///home/nameuser/.myProject/conf/”)};'? – darcyq 2013-02-21 17:18:09

回答

0

我成功

谢谢达西邱不错URL[] url={new URL("file:///home/nameuser/.myProject/conf/")};我不得不这样做,而不是URL[] url={new URL("file:///home/nameuser/.myProject/conf/myfile")};