使用URLClassLoader从文件加载类时出现ClassNotFoundException

时间:2014-07-10 20:35:00

标签: java reflection urlclassloader

我使用以下代码创建实现OdbcIniTarget接口的Target类实例。 OdbcIniTarget类已存在于应用程序中,但位于不同的包中,现在应从目标目录加载,该目录不在类路径中。

...
// directory below is resolved to /D:/Workspace/<myproject>/targets/
private File targetsLocation = new File("targets");
...
private void loadTargets() {            

    URL url = null;
    try {
        url = targetsLocation.toURI().toURL();            
    } catch (MalformedURLException e) {

    }

    URL[] urls = new URL[]{ url };        
    URLClassLoader classLoader = new URLClassLoader(urls);

    try {

     Target target =
     (Target)classLoader.
     loadClass("ch.blah.targets.nope.OdbcIniTarget").
     newInstance(); // <--- this fails hard  

    } catch (...) {
     ...
    }            

    try {
        classLoader.close();
    } catch (IOException e) {

    }
}

我收到以下异常。

java.lang.ClassNotFoundException: ch.blah.targets.nope.OdbcIniTarget
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at ch.blah.targets.TargetFactory.loadTargets(TargetFactory.java:74)
    at ch.blah.targets.TargetFactory.<init>(TargetFactory.java:41)
    at ch.blah.SomeOfMyClasses.<init>(SomeOfMyClasses.java:34)
    at ch.blah.TheMainClass.main(TheMainClass.java:38)

有人知道为什么找不到这个班级吗?

加载的类如下所示。

package ch.blah.targets.nope;

import org.apache.log4j.Logger;

import ch.blah.utils.Utils;

public class OdbcIniTarget extends Target {

    private static final Logger log = Logger.getLogger(OdbcIniTarget.class);    

    @Override
    public boolean someMethod1() {
        return false;
    }

    @Override
    public boolean someMethod2() {
        return false;
    }

    @Override
    public boolean someMethod3() {
        return false;
    }       

    @Override
    public void someMethod4() {
        log.debug("Blah.");
        Utils.getInstance().marshalClass(SomeClass.class);
        log.debug("Finished.");
    }
}

目标目录的目录结构如下。

D:\Workspace\<myproject>\targets>dir
 Volume in drive D is DATA
 Volume Serial Number is 021C-EC9B

 Directory of D:\Workspace\<myproject>\targets

10.07.2014  21:20    <DIR>          .
10.07.2014  21:20    <DIR>          ..
10.07.2014  21:31             1'278 OdbcIniTarget.java
10.07.2014  20:23             3'761 OdbcIniTargetConfiguration.java
               2 File(s)          5'039 bytes
               2 Dir(s)   7'328'571'392 bytes free

2 个答案:

答案 0 :(得分:2)

<强>问题:

无法加载,因为它不包含该URL的类,它有java源

<强>解决方案:

将这些java类编译为.class文件,然后将url传递给看起来像

的目录
ch\blah\targets\nope\OdbcIniTarget.class

答案 1 :(得分:0)

尝试将调用者的类加载器作为参数传递给构造函数:

URLClassLoader classLoader = new URLClassLoader(urls, getClass().class.getClassLoader());

该类是url中dir的直接子项,因此您不需要包:

Target target =
 (Target)classLoader.
 loadClass("OdbcIniTarget").
 newInstance();