java - Initial package of class from another direc

2019-09-15 20:05发布

问题:

I have some problem how to initial package from class file from another directory

File file=new File("D:\\java\\myproject\\Name_pack\\time\\MyClass.class");
URL[] cp =
{
    new File(file.getParent()).toURI().toURL()
};
URLClassLoader urlcl = new URLClassLoader(cp);
Class cls = urlcl.loadClass(file.getName().replaceAll("\\.class","");

if class file not contain a package, it's working.
but it's contains a package, i get some error like this :

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: Waktu (wrong name: Name_pack/time/MyClass)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

I don't know how to get package.

so, i want initial like this :

Class cls = urlcl.loadClass(Name_pack.time.MyClass);

回答1:

If all you have, is an absolute file path without knowledge about the code base nor the expected package of the class, things get complicated. The actual full qualified name is contained in the class file, that’s why the class loader rejects it; it even tells you the actual name in the exception message, however, there is no guaranty that the exception’s message will contain that information.

Since I do not assume that you want to start parsing the byte code to find the actual class name, I suggest to redesign the application to receive the class path directory (code base) and full qualified class name separately in the first place.

A simple, but not really recommended, possibility is just trying it:

File file=new File("D:\\java\\myproject\\Name_pack\\time\\MyClass.class");
String className=file.getName();
if(!className.endsWith(".class"))
    throw new IllegalArgumentException(file.toString());
className=className.substring(0, className.lastIndexOf('.'));
File codeBase=file;
Class cls = null;
do {
    codeBase=codeBase.getParentFile();
    URL[] cp = { codeBase.toURI().toURL() };
    URLClassLoader urlcl = new URLClassLoader(cp);
    try { cls = urlcl.loadClass(className); }
    catch(NoClassDefFoundError ex) {
        className=codeBase.getName()+'.'+className;
    }
} while(cls==null);

But as said, the best option is to let, whatever source provided you the file, provide the correct code base directory in the first place…