【问题标题】:how to identified class if including package from another directory如果包含来自另一个目录的包,如何识别类
【发布时间】:2015-02-05 11:04:55
【问题描述】:

来自there,我使用类加载器从其他目录识别类

File dir=new File ("D:\\dirclass")
fulldir = new File (dir+"\\myclass");
filename = new StringTokenizer(fulldir.getName(), ".").nextToken();
URL[] checkclass = 
{
    dir.toURI().toURL()
};
URLClassLoader urlcl = new URLClassLoader(checkclass);
Class cls = urlcl.loadClass(filename);

如果类没有包,这是有效的。

但是如果类有包,运行失败。

Exception in thread "main" java.lang.NoClassDefFoundError: packclass (wrongname: dirclass\packclass)

还有其他方法吗?

【问题讨论】:

  • 什么“不起作用”? 会发生什么
  • 包含包的识别类失败

标签: java class


【解决方案1】:

您必须将类的完全限定名称指定为ClassLoader,例如:

Class cls = urlcl.loadClass("com.mypackage.MyClass");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 2014-03-17
    • 2013-05-03
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    相关资源
    最近更新 更多