1

我尝试扩展 ClassLoader。我的 ClassLoader.loadClass 是:

 protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {// i put "throw"s here to clean code below

        Class<?> result = null;


            byte[] bytes = null;
            try {
                bytes = getClassFromFS(pathToClass); //get .class file from file system in bytes[]
            } catch (FileNotFoundException ex) {
                Logger.getLogger(MyLoader.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(MyLoader.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("MyLoader:мой loadClass загружает класс");
            return defineClass(name, bytes, 0, bytes.length); // the proplem is here !!!

        return super.loadClass(name, resolve); 
}

“主”线程中的行

/*first argument - path to file. File exist, I checked it*/
myClassLoader = new MyLoader("D:\\\\customclassloader\\ClassX.class", ClassLoader.getSystemClassLoader());
classX = (SimpleInterface) myClassLoader.loadClass("customclassloader.ClassX",true).newInstance();
</pre>
then I have exception 
<pre>Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.lang
at java.lang.ClassLoader.preDefineClass(ClassLoader.java:650)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:786)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:635)
    at customclassloader.MyLoader.loadClass(MyLoader.java:61)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:635)// the proplem is here !!!
    at customclassloader.MyLoader.loadClass(MyLoader.java:61)
    at customclassloader.CustomClassLoader.main(CustomClassLoader.java:32)

X 类:

package customclassloader;

/**
 *
 * @author roman
 */
public class ClassX {

    static {
        System.out.println("класс ClassX инициируеться");
    }

    public ClassX() {
        System.out.println("класс ClassX конструируеться");
    }

    public void f(){
        System.out.println("класс ClassX выполняет f();");
    }


}

我不明白。我将类命名为“customclassloader.ClassX”。为什么它显示名称“java.lang”?ClassX.class 在同一个项目和同一个包中编译。

4

2 回答 2

2

It shows java.lang because java.lang.Object is the superclass of all classes which the loader is trying to load.

you can check this link for sample http://www.javaworld.com/jw-10-1996/jw-10-indepth.html?page=2

" the next step is to check if the primordial class loader can resolve this class name. This check is essential to both the sanity and security of the system. For example, if you return your own instance of java.lang.Object to the caller, then this object will share no common superclass with any other object! The security of the system can be compromised if your class loader returned its own value of java.lang.SecurityManager, which did not have the same checks as the real one did. "

于 2013-07-31T13:04:48.917 回答
0

似乎 Java 禁止您将自己的类放入包中java.lang。并且因为您正在扩展 ClassLoader,它位于 中java.lang,所以您的类被视为java.lang包的一部分。

尝试使用package导入说明顶部的语句将其放入另一个包中

PS:答案如下所示:java.lang.SecurityException:禁止的包名称:java.lang

于 2013-07-31T08:36:58.170 回答