简体   繁体   English

加载一个位于loader里面的Java agent JAR

[英]Load a Java agent JAR located inside the loader

I have two separate projects in my IDE for the agent and for the loader that finds a target VM and loads the agent JAR.我的 IDE 中有两个单独的项目,用于代理和用于查找目标 VM 并加载代理 JAR 的加载程序。

  • When the agent project is built, the resulting agent JAR artifact is copied into the loader's resources folder.构建代理项目时,生成的代理 JAR 工件将复制到加载程序的资源文件夹中。
  • When the loader project is built, the loader JAR contains both loader code and the agent.jar in it.构建loader项目时,loader JAR既包含loader代码,也包含agent.jar

The resulting runnable loader structure looks like this:生成的可运行加载程序结构如下所示:

loader.jar
├── META-INF
│   └── MANIFEST.MF
├── me.domain.loader
│   └── Main.class
└── agent.jar
    ├── META-INF
    │   └── MANIFEST.MF
    └── me.domain.agent
        └── Agent.class

From the VirtualMachine#loadAgent(java.lang.String) specification, I need to provide a path to the JAR containing the agent as the first parameter.根据VirtualMachine#loadAgent(java.lang.String)规范,我需要提供包含代理的 JAR 的路径作为第一个参数。

However, when using Main.class.getResource("/agent.jar").getPath() I'm getting an AgentLoadException: Agent JAR not found or no Agent-Class attribute .但是,当使用Main.class.getResource("/agent.jar").getPath()我得到一个AgentLoadException: Agent JAR not found or no Agent-Class attribute What's the correct way to do that?正确的做法是什么?

I already had such issue on a maven project.我已经在 maven 项目中遇到过这样的问题。 Anyway you may need to have a manifest file here in META-INF/MANIFEST.MF:不管怎样,你可能需要在 META-INF/MANIFEST.MF 中有一个清单文件:

Manifest-Version: 1.0
Agent-Class: com.package.AgentLoader.agentNameHere
Permissions: all-permissions

You have more details here: https://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html or Agent JAR not found or no Agent-Class attribute您在此处有更多详细信息: https://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html找不到代理 JAR 或没有代理类属性

It looks like the agent JAR to be loaded must exist on disk.看起来要加载的代理 JAR 必须存在于磁盘上。 I've solved this issue by copying the embedded JAR resource into a temporary file:我通过将嵌入的 JAR 资源复制到一个临时文件中解决了这个问题:

private static String getTemporaryResource(String resourceName) {

    // Read embedded resource from this JAR
    InputStream resourceStream = Main.class.getResourceAsStream(resourceName);
    if (resourceStream == null) {
        throw new Exception("Resource not found in the JAR");
    }

    // Create a temporary file in %TEMP%/resource5513111026806316867.tmp
    File temporaryFile = File.createTempFile("resource", null);
    temporaryFile.deleteOnExit();

    // Copy the resource data into the temporary file
    Files.copy(resourceStream, temporaryFile.toPath(), StandardCopyOption.REPLACE_EXISTING);

    // Return the path to temporary file
    return temporaryFile.getAbsolutePath();
}

I'm then using this temporary path to load the agent:然后我使用这个临时路径来加载代理:

String tempAgentPath = getTemporaryResource("/agent.jar");
VirtualMachine targetVM = VirtualMachine.attach("1337");
targetVM.loadAgent(tempAgentPath);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM