[英]Get jar manifest attributes from Java agent
I am running my own java agent on a jar containing some sample code.我在包含一些示例代码的 jar 上运行我自己的 java 代理。 Input to the command line:
输入到命令行:
java -javaagent:path/to/agent.jar=path/to/main-class -jar path/to/sample-code.jar
I'm currently providing the sample-code.jar
's main class (as stated in its manifest) as an arg to the agent, although, I'm trying to find a way to access the sample-code.jar
's manifest's Main-Class attribute from within the agent's premain
.我目前正在向代理提供
sample-code.jar
的主要 class (如其清单中所述)作为 arg 给代理,但我正在尝试找到一种访问sample-code.jar
的方法。代理的premain
中的 Main-Class 属性。
I tried some of the answers suggested here: Reading my own Jar's Manifest , although, no luck - I think the approaches I tried require everything to have already been fully loaded.我尝试了这里建议的一些答案: Reading my own Jar's Manifest ,虽然没有运气 - 我认为我尝试的方法要求所有内容都已经完全加载。
Any suggestions are much appreciated.任何建议都非常感谢。
Turns out a combination of the linked question's answers worked for me (note loader
is already provided as an arg in a ClassFileTransformer
's transform
method).结果是链接问题的答案组合对我有用(注意
loader
已经在ClassFileTransformer
的transform
方法中作为 arg 提供)。
URL url = loader.getResource("META-INF/MANIFEST.MF");
if (url == null) {
throw new RuntimeException("Can't locate manifest.");
}
try {
Manifest manifest = new Manifest(url.openStream());
Attributes attr = manifest.getMainAttributes();
String mainClass = attr.getValue("Main-Class").replace(".", "/");
} catch (IOException e) {
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.