本文介绍了java自定义类加载器:我的类加载器未加载某些类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写自定义类加载器,我已经通过使用参数将其设置为默认类加载器

i'm writing a custom classloader, i'have set it to be the default classloader by using the parameter

-Djava.system.class.loader=MyClassLoader

大多数类是由我的类加载器加载的,但是有些不上课,为什么?
该类到一个外部jar文件中。

Most of the classes are loaded by my classloader, but some classes not, why?This classes are into an external jar file.

UPDATE
这里是一个示例

UPDATEHere an example

public class Main{
    public static void main(String[] args) {
        try{
            // A simple class loader, ovveride loadClass
            // method and print in stdout the name of the class loaded.
            MyClassLoader classLoader=new MyClassLoader(MyClassLoader.class.getClassLoader());
            Class init=classLoader.loadClass("Initializer");
            Object instance=init.newInstance();
            init.getMethod("init").invoke(instance);
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
}

public class A{

    public A() {
        System.out.println("Im A");
    }
}

public class Initializer {

     public void init() {
        A a=new A();
    }
}

问题是:我希望加载A类

The problem is: I expect that class A are loaded by my class loader, but this is does not happen, why?

UPDATE

无论如何,我想用我的类加载器加载我所有的类,因为我想加密类代码并在运行时解密它。
那么,如何将我的类加载器用作所有类的默认类加载器?

Anyway, i want to load ALL my classes with my class loader, becouse i want to encrypt class code and decrypt it at runtime.So, how can i use my class loader as default class loader for ALL my classes?

谢谢。

推荐答案

java.lang 下的所有内容始终由引导类加载器加载。

Anything under java.lang will always be loaded by the bootstrap classloader.

来自:


  1. 引导类加载器

  2. 扩展类加载器

  3. 系统类加载器

引导类加载器加载
核心Java库[5]
(/ lib目录)。这个
类加载器是
核心JVM的一部分,是用本机代码编写的。

The bootstrap class loader loads the core Java libraries[5] (/lib directory). This class loader, which is part of the core JVM, is written in native code.

扩展类加载器加载$ b $扩展目录
(/ lib / ext或
java.ext.dirs系统属性指定的任何其他
目录)中的b代码。它是
,由
sun.misc.Launcher $ ExtClassLoader
类实现。

The extensions class loader loads the code in the extensions directories (/lib/ext or any other directory specified by the java.ext.dirs system property). It is implemented by the sun.misc.Launcher$ExtClassLoader class.

这篇关于java自定义类加载器:我的类加载器未加载某些类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 06:50