문제

community! Would be great if u could help me w/ my issue.

I got a custom class loader which is gonna to be java.system.class.loader - it holds urls where to find classes. Like this:

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);
    }
    }
}

Then I run the java(JDK6) with -Djava.system.class.loader=TestSystemClassLoader eg.TestMain, where eg.TestMain' main:

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);
}

The thing which makes me mad is that if I unpack/unzip/unjar the junit-4.8.2.jar - then eg.TestMain would work!

The question is - how to tell java(JDK6) that I want whole directory to be in classpath, i.e. any file residing in the directory.

Thanks in advance!

도움이 되었습니까?

해결책

Find all the jars and add them:

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);
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top