java加载.class文件_Java Language

Example

To load a class we first need to define it. The class is defined by the ClassLoader. There's just one problem, Oracle didn't write the ClassLoader's code with this feature available. To define the class we will need to access a method named defineClass() which is a private method of the ClassLoader.

To access it, what we will do is create a new class, ByteClassLoader, and extend it to ClassLoader. Now that we have extended our class to ClassLoader, we can access the ClassLoader's private methods. To make defineClass() available, we will create a new method that will act like a mirror for the private defineClass() method. To call the private method we will need the class name, name, the class bytes, classBytes, the first byte's offset, which will be 0 because classBytes' data starts at classBytes[0], and the last byte's offset, which will be classBytes.lenght because it represents the size of the data, which will be the last offset.

public class ByteClassLoader extends ClassLoader {

public Class> defineClass(String name, byte[] classBytes) {

return defineClass(name, classBytes, 0, classBytes.length);

}

}

Now, we have a public defineClass() method. It can be called by passing the name of the class and the class bytes as arguments.

Let's say we have class named MyClass in the package stackoverflow...

To call the method we need the class bytes so we create a Path object representing our class' path by using the Paths.get() method and passing the path of the binary class as an argument. Now, we can get the class bytes with Files.readAllBytes(path). So we create a ByteClassLoader instance and use the method we created, defineClass(). We already have the class bytes but to call our method we also need the class name which is given by the package name (dot) the class canonical name, in this case stackoverflow.MyClass.

Path path = Paths.get("MyClass.class");

ByteClassLoader loader = new ByteClassLoader();

loader.defineClass("stackoverflow.MyClass", Files.readAllBytes(path);

Note: The defineClass() method returns a Class> object. You can save it if you want.

To load the class, we just call loadClass() and pass the class name. This method can throw an ClassNotFoundException so we need to use a try cath block

try{

loader.loadClass("stackoverflow.MyClass");

} catch(ClassNotFoundException e){

e.printStackTrace();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值