`

Java Class ClassLoader

    博客分类:
  • java
阅读更多

    下午在看《Java 深度历险》,对Class & ClassLoader的定位不是很clear,于是不小心搜到如下一篇blog:

http://www.cnblogs.com/pony/archive/2008/10/10/1307921.html

   blog_name : Java动态加载类  把代码稍作修改,就能跑起来了。ok,now show code:

共4个class--

package com.mark.core.test;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Hashtable;

public class MyClassLoader extends ClassLoader {
    // 定义哈希表(Hashtable)类型的变量,用于保存被载入的类数据。
    Hashtable loadedClasses;

    public MyClassLoader() {
        loadedClasses = new Hashtable();
    }

    @Override
    public synchronized Class loadClass(String className, boolean resolve) throws ClassNotFoundException {
        Class newClass;
        byte[] classData;

        // 检查要载入的类数据是否已经被保存在哈希表中。
        newClass = (Class) loadedClasses.get(className);
        // 如果类数据已经存在且resolve值为true,则解析它。
        if (newClass != null) {
            if (resolve) {
                resolveClass(newClass);
            }
            return newClass;
        }

        /*
         * 首 先 试 图 从 本 地 系 统 类 组 中 载 入 指 定 类。 这 是 必 须 的, 因 为 虚 拟 机 将 这 个 类 载 入 后, 在 解 析 和 执 行 它 时 所 用 到 的 任 何 其 他 类,
         * 如java.lang.System 类 等, 均 不 再 使 用 虚 拟 机 的 类 载 入 器, 而 是 调 用 我 们 自 制 的 类 载 入 器 来 加 载。
         */

        try {
            newClass = findSystemClass(className);
            return newClass;
        } catch (ClassNotFoundException e) {
            System.out.println(className + " is not a system class!");
        }

        // 如果不是系统类,则试图从网络中指定的URL地址载入类。
        try {
            // 用自定义方法载入类数据,存放于字节数组classData中。
            classData = getClassData(className);
            // 由字节数组所包含的数据建立一个class类型的对象。
           // newClass = defineClass(classData, 0, classData.length);
            newClass = defineClass(null, classData, 0, classData.length);
            if (newClass == null) {
                throw new ClassNotFoundException(className);
            }
        } catch (Exception e) {
            throw new ClassNotFoundException(className);
        }

        // 如果类被正确载入,则将类数据保存在哈希表中,以备再次使用。
        loadedClasses.put(className, newClass);
        // 如果resolve值为true,则解析类数据。
        if (resolve) {
            resolveClass(newClass);
        }
        return newClass;
    }

    // 这个方法从网络中载入类数据。
    protected byte[] getClassData(String className) throws IOException {
        byte[] data;
        int length;
        try {
            // 从网络中采用URL类的方法载入指定URL地址的类的数据。
            URL url = new URL(className.endsWith(".class") ? className : className + ".class");
            URLConnection connection = url.openConnection();
            InputStream inputStream = connection.getInputStream();
            length = connection.getContentLength();

            data = new byte[length];
            inputStream.read(data);
            inputStream.close();
            return data;
        } catch (Exception e) {
            System.out.println("================start:");
            e.printStackTrace();
            System.out.println("================end:");
            throw new IOException(className);
        }
    }
}

  interface:

 

package com.mark.core.test;

public interface Share {
    public void start(String[] option);
}

 

client test class:

 

package com.mark.core.test;

public class TestClassLoader {
    public static void main(String[] args) {
        MyClassLoader ll = new MyClassLoader();
        Class cc;
        Object oo;
        String ss = "http://localhost:8080/webdemo2/classes/RemoteClass.class";

        if (args.length != 0) {
            ss = args[0];
        }
        try {
            System.out.println("Loading class " + ss + "...");
            // 使用重写的方法loadClass()载入类数据。
            cc = ll.loadClass(ss);
            System.out.println("Creat instance...");
            // 创建Object类型的类实例。
            oo = cc.newInstance();
            System.out.println("Call start() method...");
            // 强制类型转换并执行被载入类中的方法。
            ((Share) oo).start(args);
        } catch (Exception e) {
            System.out.println("Caught exception : " + e);
            e.printStackTrace();
        }
    }
}

 

the file below should be deploy in server after compiler it.

package com.mark.core.test;


public class RemoteClass implements Share {

    /* (non-Javadoc)
     * @see com.mark.core.test.Share#start(java.lang.String[])
     */
    @Override
    public void start(String[] option) {
        System.out.println("congratulations!");
    }

}

 

how to?

step 1: build a java application project [A]. add the code above.

step 2: build a java_server project [B] that it can run on tomcat or other servers.

step 3: in java_server project : then you can  copy RemoteClass.class from project A to project B And don't forget to delete the RemoteClass.java in Project A.

step 4:

   update code.

 

String ss = "http://localhost:8080/webdemo2/classes/RemoteClass.class";

 

 

分享到:
评论

相关推荐

    破解java加密的ClassLoader.java,在classloader植入破解代码

    破解java加密的ClassLoader.java,在classloader植入破解代码

    Java实现热加载完整代码;Java动态加载class;Java覆盖已加载的class;Java自定义classloader

    让Java支持热加载是个不错的想法。如何做到的呢? 1. 定义好接口和实现类 2. 让代理类通过反射的方式调用实现类,对外暴露的是代理类。 3. 自定义URLClassLoader。检查实现类.class文件的...Java自定义classloader;

    Java_ClassLoader详解

    Java_ClassLoader详解,解说java类的加载的原理,让你轻松了解java的类加载

    java class加密保护工具

    本工具是对java class文件进行加密保护的工具!本工具全面支持linux/unix/windows操作系统。 众所周知,java编译后的class文件是一种中间字节字文件, 很容易被反编译工具反编译,而传统的java源代码保护方法基本都是...

    破解java加密的rt.jar,在classloader植入破解代码

    破解java加密的rt.jar,在classloader植入破解代码,默认输出到c:/TEMP/classes/目录。使用方法:只要下载本rt.jar,然后替换掉jdk1.8.0_25\jre\lib目录下的rt.jar。然后运行你需要破解的java程序即可,如果你的java...

    java class加密保护(完全免费) v2.1

    本工具是对java class文件进行加密保护防止反编译的工具!本工具全面支持linux/unix/windows操作系统。 继推出v1.0版本后,获得了用户大量的支持与的反馈,我们再次推出本v2.0版,对加密算法进行了更大的改进,安全...

    Java Classloading Mechanism : ClassLoader & ASM & 动态字节码增强

    NULL 博文链接:https://wuaner.iteye.com/blog/1011036

    Java类加密程序

    执行java时带上参数 -agentlib:<动态库文件所在路径>\hidea 注意:不要加文件后缀.dll,直接使用文件的名字部分(classloader)! <br>举例说明:例如,本加密工具安装在c:\hideasoft\java_protect,执行...

    Java类加密2.0版本,无限制

    JAVA CLASS文件加密工具是一款专门为保护您的JAVA源代码而设计的软件。传统的JAVA代码保护方式通常是扰乱生成的CLASS文件,从而降低反编译生成的源代码的可读性;有的保护工具甚至能生成部分废代码(垃圾代码),...

    resolver_java_wsdl.jar

    at java.lang.ClassLoader.loadClass(ClassLoader.java:667) at com.ibm.ws.bootstrap.ExtClassLoader.loadClass(ExtClassLoader.java:119) at java.lang.ClassLoader.loadClass(ClassLoader.java:650) at ...

    对Java的ClassLoader的几点认识

    1、ClassLoader只是一个普通抽象类,它的工作是从类名得到Class。ClassLoader与其他类的不同之处是它是为JVM服务的,属于“公务员”,例如Thread带有getContextClassLoader()和setContextClassLoader()方法;  2、...

    aop面向切面需要的jar包

    at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at java.lang....

    Java classloader原理深究

    前面已经写过一篇关于java classloader的拙文java classloader原理初探。  时隔几年,再看一遍,觉得有些地方显得太过苍白,于是再来一篇:  完成一个Java类之后,经过javac编译,会生成一个class文件,这个...

    ClassLoader 案例

    自定义ClassLoader,控制台输入调试。 运行期间 重新载入指定目录的class文件。可实现对于类的功能函数更新。 用到java 反射,@interface 等技术

    java类加密工具v2.1

    本工具是对java class文件进行加密保护防止反编译的工具!本工具全面支持linux/unix/windows操作系统。 继推出v1.0版本后,获得了用户大量的支持与的反馈,我们再次推出本v2.0版,对加密算法进行了更大的改进,安全...

    深入java虚拟机(inside the java virtual machine)

    A Reference to Class ClassLoader A Reference to Class Class Method Tables An Example of Method Area Use The Heap Garbage Collection Object Representation Array Representation The Program ...

    Java加载。jar包

    在java.lang包里有个ClassLoader类,ClassLoader 的基本目标是对类的请求提供服务。当 JVM 需要使用类时,它根据名称向 ClassLoader 请求这个类,然后 ClassLoader 试图返回一个表示这个类的 Class 对象。通过覆盖...

    Java ClassLoader 原理详细分析

    一、什么是ClassLoader?...而程序在启动的时候,并不会一次性加载程序所要用的所有class文件,而是根据程序的需要,通过Java的类加载机制(ClassLoader)来动态加载某个class文件到内存当中的,从而只有clas

    jboss 5 原理 2 classloader

    With OSGi-style classloading getting more and more traction, and a number of new Java modules/classloading specifications on the horizon, it was high time we revamped our classloading layer in order ...

    在Java的反射中,Class.forName和Class

    在Java的反射中,Class.forName和ClassLoader的区别共4页.pdf.zip

Global site tag (gtag.js) - Google Analytics