[英]Load a Java agent JAR located inside the loader
我的 IDE 中有兩個單獨的項目,用於代理和用於查找目標 VM 並加載代理 JAR 的加載程序。
agent.jar
。生成的可運行加載程序結構如下所示:
loader.jar
├── META-INF
│ └── MANIFEST.MF
├── me.domain.loader
│ └── Main.class
└── agent.jar
├── META-INF
│ └── MANIFEST.MF
└── me.domain.agent
└── Agent.class
根據VirtualMachine#loadAgent(java.lang.String)規范,我需要提供包含代理的 JAR 的路徑作為第一個參數。
但是,當使用Main.class.getResource("/agent.jar").getPath()
我得到一個AgentLoadException: Agent JAR not found or no Agent-Class attribute
。 正確的做法是什么?
我已經在 maven 項目中遇到過這樣的問題。 不管怎樣,你可能需要在 META-INF/MANIFEST.MF 中有一個清單文件:
Manifest-Version: 1.0
Agent-Class: com.package.AgentLoader.agentNameHere
Permissions: all-permissions
您在此處有更多詳細信息: https://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html或找不到代理 JAR 或沒有代理類屬性
看起來要加載的代理 JAR 必須存在於磁盤上。 我通過將嵌入的 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();
}
然后我使用這個臨時路徑來加載代理:
String tempAgentPath = getTemporaryResource("/agent.jar");
VirtualMachine targetVM = VirtualMachine.attach("1337");
targetVM.loadAgent(tempAgentPath);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.