⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testjar.java

📁 This is source run loader with java
💻 JAVA
字号:
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.util.Hashtable;
import java.util.Map;
import java.util.jar.JarInputStream;
import java.util.zip.ZipEntry;

public class TestJar {

    public static void main(String[] args) throws Exception {
        FileInputStream in = new FileInputStream("test.jar");
        byte[] jarResource = new byte[in.available()];
        in.read(jarResource);
        JarLoader jarLoader = new JarLoader(jarResource);
        Class newClass = jarLoader.getNewClass("test.HelloWorld");
        Object object = newClass.newInstance();
        //object.equals se sinh ra cau chao Hello : test.HelloWorld
        System.out.println(object.equals(object));
    }
}

class JarResource {
    Map m_jarContents = new Hashtable();

    public JarResource(byte[] jarSource) throws Exception {
        loadJarStreamContents(jarSource);
    }

    private void loadJarStreamContents(byte[] jarData) throws Exception {
        ByteArrayInputStream bytStream = new ByteArrayInputStream(jarData);
        BufferedInputStream bis = new BufferedInputStream(bytStream);
        JarInputStream zFile = new JarInputStream(bis);
        ZipEntry ze = zFile.getNextEntry();
        while (ze != null) {
            if (ze.isDirectory()) {
                ze = zFile.getNextEntry();
                continue;
            }

            byte[] data = new byte[1024];
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int nLen;
            do {
                nLen = zFile.read(data);
                if (nLen < 0)
                    break;
                out.write(data, 0, nLen);
            } while (true);
            /* prepare repository */
            if (out.size() > 0) {
                byte resource[] = out.toByteArray();
                /* stow the resource full path */
                m_jarContents.put(ze.getName(), resource);
                /* stow the resource filename only */
                int i = ze.getName().lastIndexOf('/');
                if (i > 0) {
                    String key = ze.getName().substring(i + 1, ze.getName().length());
                    m_jarContents.put(key, resource);
                }
            }

            ze.clone();
            ze = zFile.getNextEntry();
        }
        zFile.close();
    }
}

class JarLoader extends ClassLoader {

    JarResource m_jarResource;

    public JarLoader(byte[] jarSource) throws Exception {
        m_jarResource = new JarResource(jarSource);
    }

    public Class getNewClass(String sClassName) {
        String sTmpClassName = formatClassName(sClassName, true);
        byte[] classSource = (byte[]) m_jarResource.m_jarContents.get(sTmpClassName);
        return defineClass(sClassName, classSource, 0, classSource.length);
    }
    
    protected String formatClassName(String sClassName, boolean bRealName) {
        if(sClassName.endsWith(".class"))
            sClassName = sClassName.substring(0, sClassName.length()-6);
        if(bRealName)
            sClassName = sClassName.replace('.', '/') + ".class";
        return sClassName;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -