2013-02-21 134 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")};