将 jar 文件添加到检测路径

2024-02-14

我有两个 jar 文件(例如,我们将它们称为 Updater.jar 和 Code.jar)。 Updater.jar 使用其 main 方法启动,然后使用 premain 方法再次启动自身:

package Update;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class InstructionLauncher {

    private List<UpdateInstruction> instructions = new ArrayList<UpdateInstruction>();
    private static InstructionLauncher instance;
    private Process process;

    public static InstructionLauncher initialise(){
        if(instance !=null) return instance;
        else return new InstructionLauncher();
    }

    public void registerPremain(UpdateInstruction inst){
        instructions.add(inst);
    }

    public void launchNext(){
        UpdateInstruction inst = instructions.get(0);
        String cls = inst.getClassName() + "." + inst.getMethodName();
        String[] args = new String[]{"java", "-javaagent", "JOSUpdater.jar", "-jar", inst.getClassName() + "." + inst.getMethodName()};
        ProcessBuilder builder = new ProcessBuilder(args);
        try {
            exportResource(cls, cls);
        } catch (Exception e) {
            UpdateManager.revert();
        }
        try {
            Process p = builder.start();
            process = p;
        } catch (IOException e) {
            e.printStackTrace();
        }
        while(!process.isAlive())launchNext();
    }

    private InstructionLauncher(){
        instance = this;
    }

    //From http://stackoverflow.com/questions/10308221/how-to-copy-file-inside-jar-to-outside-the-jar
    private String exportResource(String resourceName, String clazz) throws Exception {
        InputStream stream = null;
        OutputStream resStreamOut = null;
        String jarFolder;
        try {
            stream = Class.forName(clazz).getResourceAsStream(resourceName);//note that each / is a directory down in the "jar tree" been the jar the root of the tree
            if(stream == null) {
                throw new Exception("Cannot get resource \"" + resourceName + "\" from Jar file.");
            }

            int readBytes;
            byte[] buffer = new byte[4096];
            jarFolder = new File(Class.forName(clazz).getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile().getPath().replace('\\', '/');
            resStreamOut = new FileOutputStream(jarFolder + resourceName);
            while ((readBytes = stream.read(buffer)) > 0) {
                resStreamOut.write(buffer, 0, readBytes);
            }
        } catch (Exception ex) {
            throw ex;
        } finally {
            stream.close();
            resStreamOut.close();
        }

        return jarFolder + resourceName;
    }

}

premain 方法现在看起来像这样:

package Update;

import java.lang.instrument.Instrumentation;

public class PremainLauncher {

    public static void premain(String args, Instrumentation inst){
        inst.addTransformer(new Transformer(), true);
        System.out.println("Registered instruction for package: " + args);
    }

}

我想知道如何将整个外部 JAR(本例中为 Code.jar)添加到仪器的路径中?

我知道 Instrumentation.retransformClasses 方法,但要使用该方法,我需要获取 jar 中所有类的 List> ,但我无法完成该操作。

假设 Code.jar 有三个类文件:Main.class、writer.class 和 display.class。有没有办法获取每个类对象的列表,而不是他们的名字?


Java 代理可以简单地通过以下方式添加 jar 文件Instrumentation https://docs.oracle.com/javase/8/docs/api/?java/lang/instrument/Instrumentation.html它在启动方法中接收到的接口,例如

import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.util.jar.JarFile;

public class PremainLauncher {
    public static void premain(String args, Instrumentation inst) throws IOException{
        inst.appendToSystemClassLoaderSearch(new JarFile("Code.jar"));
        inst.addTransformer(new Transformer(), true);
        System.out.println("Registered instruction for package: " + args);
    }
}

See Instrumentation.appendToSystemClassLoaderSearch(…) https://docs.oracle.com/javase/8/docs/api/java/lang/instrument/Instrumentation.html#appendToSystemClassLoaderSearch-java.util.jar.JarFile-。如果您还打算以被检测的类需要访问以下类的方式来检测 JRE 类:Code.jar,你必须更改引导路径 https://docs.oracle.com/javase/8/docs/api/java/lang/instrument/Instrumentation.html#appendToBootstrapClassLoaderSearch-java.util.jar.JarFile-反而。

请注意,这必须尽早发生:

Java™ 虚拟机规范指定后续尝试解析 Java 虚拟机之前未成功尝试解析的符号引用始终会失败,并出现与初始解析尝试所引发的相同错误。因此,如果 JAR 文件包含与 Java 虚拟机尝试解析引用失败的类相对应的条目,则解析该引用的后续尝试将失败,并出现与初始尝试相同的错误。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 jar 文件添加到检测路径 的相关文章

随机推荐