[英]how to identified class if including package from another directory
從那里 ,我使用類加載器從其他目錄中識別類
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)
還有其他辦法嗎?
您必須為ClassLoader
指定類的完全限定名稱,例如:
Class cls = urlcl.loadClass("com.mypackage.MyClass");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.