惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - FallingAutumn

mysql常用命令集锦 Java ClassLoader加载机制理解 myeclipse 8.5破解方法 Replace Pioneer 试用推广 JAVA RMI调用实战学习 linux下关于压缩、解压相关的操作 关于hessian接口类方法顺序及对象序列化的实战研究 Java对象引用传递探索 mysql 语句or效率问题 Java异常学习总结 log4j配置详解 vs2005 sp1补丁安装,报1718错误: 数字签名拒绝 无法访问Windows Installer服务 之错误解决 打开word时,”无法注册这篇文档,不能创建从其他文档到这篇文档的链接“ 错误的解决 Oracle中DBMS_JOB.SUBMIT的用法说明 vs2008 sp1如何修复、删除以及增加组件的方法 OCI-22053: 溢出错误 的原因和解决方案 statspack的实战学习 - FallingAutumn - 博客园 ORACLE中,Schema的理解
Java ClassLoader加载机制理解 实际例子
FallingAutumn · 2015-02-04 · via 博客园 - FallingAutumn

针对 Java ClassLoader加载机制理解, 做了个如何自定制简单的ClassLoader,并成功加载指定的类。

不废话,直接上代码。

package com.chq.study.cl;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

/**
 * @desc 自定义ClassLoader,只能加载.class结尾的,用来测试java的classLoader机制
 */
public class ChqClassLoader extends ClassLoader {
    private String fileName;

    public ChqClassLoader(String fileName) {
        this.fileName = fileName;
    }

    protected Class<?> findClass(String className) throws ClassNotFoundException {
        Class<?> clazz = this.findLoadedClass(className);
        if (null == clazz) {
            try {
                String classFile = getClassFile(className);
                System.out.println("findClass " + classFile);
                FileInputStream fis = new FileInputStream(classFile);
                FileChannel fileC = fis.getChannel();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                WritableByteChannel outC = Channels.newChannel(baos);
                ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
                while (true) {
                    int i = fileC.read(buffer);
                    if (i == 0 || i == -1) {
                        break;
                    }
                    buffer.flip();
                    outC.write(buffer);
                    buffer.clear();
                }
                fis.close();
                byte[] bytes = baos.toByteArray();

                clazz = defineClass(className, bytes, 0, bytes.length);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return clazz;
    }

    private String getClassFile(String name) {
        StringBuffer sb = new StringBuffer(fileName);
        name = name.replace('.', File.separatorChar) + ".class";
        sb.append(File.separator + name);
        return sb.toString();
    }
}
package com.chq.study.cl;

/**
 * @desc 自我介绍测试接口类,塑型用的
 */
public interface ITest {
    public void self();
}
package com.chq.study.cl;

/**
 * @desc 未从接口类继承
 */
public class Test {
    public void self() {
        System.out.println("this is from Test instance " + this);
    }
}
package com.chq.study.cl;

/**
 * @desc 自我介绍测试实现类
 */
public class TestImpl implements ITest {

    /* (non-Javadoc)
     * @see com.chq.study.cl.ITest#self()
     */
    @Override
    public void self() {
        System.out.println("this is from TestImpl instance " + this);
    }

} 
package com.chq.study.cl;

/**
 * @author chenqing
 * @datetime 2015年2月4日 下午4:54:12
 * @desc 入口类, 调用自定义的ClassLoader,来加载类进行验证
 */
public class MainClassLoader {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ChqClassLoader cl = new ChqClassLoader("C:\\workspaces\\MyEclipse Professional 2014\\classloader\\bin");
        try {
            Class<?> clazz = cl.findClass("com.chq.study.cl.Test");
            try {
                // 此处执行会抛出异常,验证了classLoader的全盘负责机制
                Test cc = (Test) clazz.newInstance();
                cc.self();
                System.out.println("belong class loader: " + cc.getClass().getClassLoader().toString());
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassCastException e) {
                e.printStackTrace();
            }
            
            // 确保输出顺序
            try {
                Thread.sleep(100); 
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
            
            clazz = cl.findClass("com.chq.study.cl.TestImpl");
            try {
                // 此处正常,通过塑性为基类来绕开全盘负责机制
                ITest ic = (ITest)clazz.newInstance();
                ic.self();
                System.out.println("belong class loader: " + ic.getClass().getClassLoader().toString());
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassCastException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}

最终输出的结果表明, Test & TestImpl 都找到了,但在实例化时,因为不同的classLoader加载的,导致前者失败,后者则通过塑型为基类而成功加载并实例化。

findClass C:\workspaces\MyEclipse Professional 2014\classloader\bin\com\chq\study\cl\Test.class
java.lang.ClassCastException: com.chq.study.cl.Test cannot be cast to com.chq.study.cl.Test
    at com.chq.study.cl.MainClassLoader.main(MainClassLoader.java:22)
findClass C:\workspaces\MyEclipse Professional 2014\classloader\bin\com\chq\study\cl\TestImpl.class
this is from TestImpl instance com.chq.study.cl.TestImpl@173a10f
belong class loader: com.chq.study.cl.ChqClassLoader@14318bb