自我注入的 Java 代理

huangapple go评论65阅读模式
英文:

Self-injecting java agent

问题

我正在尝试创建一个能自我注入的Java代理(java agent)。代理和动态加载代理的代码将包含在同一个JAR文件中。

我尝试通过在清单(manifest)文件中同时定义 Main-ClassAgent-Class 来实现此目标。以下是该文件内容:

Manifest-Version: 1.0
Main-Class: myjar.injector
Agent-Class: myjar.agent

然而,当我运行该JAR文件时,出现以下错误:
com.sun.tools.attach.AgentLoadException: 找不到代理 (Agent) JAR 文件或找不到 Agent-Class 属性

当我检查编译后的JAR文件时,发现只有一个 Main-Class 属性在JAR文件的清单中。我尝试在我的集成开发环境(IDE)之外重新编译,但仍然只有 Main-Class 属性。

是否有任何方法可以在一个JAR文件中同时包含 Main-ClassAgent-Class 两者呢?

英文:

I am trying to create a self-injecting java agent. The agent and the code to dynamically load the agent would be contained in the same jar file.

I tried to do this by defining both Main-Class and Agent-Class in the manifest file. This is the file:

Manifest-Version: 1.0
Main-Class: myjar.injector
Agent-Class: myjar.agent

However, I get the following error when I run the jar:
com.sun.tools.attach.AgentLoadException: Agent JAR not found or no Agent-Class attribute

When I inspected the compiled jar, there was only a Main-Class attribute in the jar's manifest file. I tried recompiling outside of my IDE, but there was still only the Main-Class attribute.

Is there any way to have both Main-Class and Agent-Class in a jar file?

答案1

得分: 1

可以的。就放在那里。您有某个将Java源文件转换为JAR文件的过程。我希望它是Maven、Gradle或其他构建系统。它也可能是集成开发环境(IDE)或自己编写的脚本。这没有什么神奇的地方:您的工具链搞乱了这个。

您始终可以按以下方式检查清单:

jar xvf myjar.jar META-INF/MANIFEST.MF
cat META-INF/MANIFEST.MF
英文:

Yes. Just put it there. You have some process that turns java source files into a jar file. I would hope it is maven or gradle or some other build system. It might be an IDE or a handrolled script. There's nothing magical about it: Your tool chain is messing this up.

You can always check the manifest as follows:

jar xvf myjar.jar META-INF/MANIFEST.MF
cat META-INF/MANIFEST.MF

huangapple
  • 本文由 发表于 2020年8月22日 07:32:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63531215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定