2011-09-20 6 views
0

커뮤니티! u가 내 문제를 도와 줄 수 있다면 좋을 것입니다.dir에서 클래스를로드 할 수 없습니다. 항아리와 함께 항아리를 압축 해제 할 때 클래스를로드 할 수 있습니다

나는 클래스를 찾을 수있는 URL을 보유하고 있습니다. java.system.class.loader이 될 사용자 정의 클래스 로더가 있습니다.

public class TestSystemClassLoader extends URLClassLoader { 

    public TestSystemClassLoader(ClassLoader parent) { 
    super(classpath(), parent); 
    } 

    private static URL[] classpath() { 
    try { 
     // I got junit-4.8.2.jar under this url. 
     URL url = new File("D:\\Work\\lib\\junit-4\\").toURI().toURL(); 
     return new URL[] { url }; 
    } 
    catch (MalformedURLException e) { 
     throw new IllegalArgumentException(e); 
    } 
    } 
} 

가 그럼 난 -Djava.system.class.loader = TestSystemClassLoader eg.TestMain, 와 자바 (JDK6)를 실행할 경우 eg.TestMain '주 :

public static void main(String[] args) throws Exception { 
    // here I got system CL which is what I want. 
ClassLoader cl = Thread.currentThread().getContextClassLoader(); 
    // here I got: "Exception in thread "main" java.lang.ClassNotFoundException: org.junit.runners.JUnit4" 
Class<?> clazz = Class.forName("org.junit.runners.JUnit4", true, cl); 
} 

것은이처럼 저를 화나게하는 이유는 junit-4.8.2.jar의 압축을 풀거나 압축 해제/풀기를하면 eg.TestMain이 작동합니다!

질문은 - 전체 디렉토리가 클래스 경로에 있기를 Java (JDK6)에 알려주는 방법, 즉 , 즉 디렉토리에있는 모든 파일을 지정하는 방법입니다.

미리 감사드립니다.

답변

0

모든 단지를 찾아 추가 :

private static URL[] classpath() { 
    try { 
     File file = new File("D:\\Work\\lib\\junit-4\\"); 
     List<URL> urls = new ArrayList<URL>(); 
     for (File f : file.listFiles()) { 
      if (f.isFile() && f.getName().endsWith(".jar")) { 
       urls.add(f.toURI().toURL()); 
      } 
     } 

     return urls.toArray(new URL[0]); 
    } 
    catch (MalformedURLException e) { 
     throw new IllegalArgumentException(e); 
    } 
}