欢迎光临散文网 会员登陆 & 注册

【JVM】Java双亲委派、类加载器这块算是玩明白了

2023-07-21 15:41 作者:推荐算法从入门到入土  | 我要投稿
import java.io.InputStream;
import java.sql.SQLOutput;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class mytest {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        ClassLoader classLoader = new ClassLoader() {

            @Override
            public Class<?> loadClass(String name) throws ClassNotFoundException {

                try {
                 String fileName =  name.substring(name.lastIndexOf(".") + 1) + ".class";
                    InputStream is = getClass().getResourceAsStream(fileName);
                    if(is == null){
                        return super.loadClass(name);
                    }
                    byte[] bytes = new byte[is.available()];
                    is.read(bytes);
                    return defineClass(name,bytes,0,bytes.length);

                }catch (Exception e )
                {
                    e.printStackTrace();
                    throw  new ClassNotFoundException(name);
                }
            }
        };
        Object user = classLoader.loadClass("User").newInstance();
        System.out.println(user.getClass());
        System.out.println(user instanceof  User);


    }
    public static String getNowTime(Long time){
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
    }
}


【JVM】Java双亲委派、类加载器这块算是玩明白了的评论 (共 条)

分享到微博请遵守国家法律