英文:
Self-injecting java agent
问题
我正在尝试创建一个能自我注入的Java代理(java agent)。代理和动态加载代理的代码将包含在同一个JAR文件中。
我尝试通过在清单(manifest)文件中同时定义 Main-Class
和 Agent-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-Class
和 Agent-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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论