使用URLClassLoader从文件加载类时发生ClassNotFoundException异常



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

...
// 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.");
    }
}

targets目录结构如下:

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

问题:

加载失败,因为它没有在那个URL中包含类,它有java源

解决方案:

将这些java类编译为。class文件,然后将url传递到目录,该目录应该看起来像

chblahtargetsnopeOdbcIniTarget.class

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

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

类是url中dir的直接子类,所以不需要包:

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

相关内容

  • 没有找到相关文章

最新更新