使用-javaagent启动JVM时,如何在manifest.mf中指定我的Premain-class

时间:2022-09-10 23:52:37

I'm currently trying to experiment with Java Instrumentation, and I'm having trouble even starting the JVM with the -javaagent arg and getting a ClassNotFoundException.

我目前正在尝试使用Java Instrumentation,我甚至无法使用-javaagent arg启动JVM并获得ClassNotFoundException。

I have a simple test project called TestInstrumentation. It has a src folder with a package called testinstrumentation. Inside is: TestInstrumentation.java and TestAgent.jar.

我有一个名为TestInstrumentation的简单测试项目。它有一个src文件夹,其中包含一个名为testinstrumentation的包。里面是:TestInstrumentation.java和TestAgent.jar。

Here is the contents of my TestAgent.jar's manifest.mf:

这是我的TestAgent.jar的manifest.mf的内容:

Manifest-Version: 1.0
Premain-Class: TestAgent
Created-By: 1.8.0_45 (Oracle Corporation)

TestAgent.java:

TestAgent.java:

package testinstrumentation;

import java.lang.instrument.Instrumentation;

public class TestAgent {
    public static void premain(String agentArgument, Instrumentation instrument) {
        System.out.println("Java Agent Loaded!");
    }
}

TestInstrumentation.java:

TestInstrumentation.java:

package testinstrumentation;

public class TestInstrumentation {
    public static void main(String[] args) {
        System.out.println("Main Class");
    }

}

Here is the stacktrace when I try to run it:

这是我尝试运行它时的堆栈跟踪:

java.lang.ClassNotFoundException: TestAgent
FATAL ERROR in native method: processing of -javaagent failed
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:304)
    at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
Exception in thread "main" Java Result: 1

I'm pretty sure the error is in my specification of the Premain-class in the manifest.mf of my jar. Any suggestions as to how I could get this corrected would be appreciated!

我很确定错误是在我的jar的manifest.mf中的Premain-class规范中。任何关于我如何得到纠正的建议将不胜感激!

1 个解决方案

#1


3  

It sounds more like a packaging issue. In general you do it right: You have to specify the following in your manifest.

这听起来更像是一个包装问题。通常,您可以正确执行此操作:您必须在清单中指定以下内容。

Premain-Class: testinstrumentation.TestAgent

Of course, the testinstrumentation.TestAgent class file should reside in the same jar. From your stacktrace I see that it looks in src.testinstrumentation however your code is supposed to be put into testinstrumentation package

当然,testinstrumentation.TestAgent类文件应该驻留在同一个jar中。从你的堆栈跟踪我看到它在src.testinstrumentation中看起来然而你的代码应该被放入testinstrumentation包中

I recommend you to read the Not so secret java agents series of tutorials (4 parts). This provides a pretty good overview of capabilities of java agents.

我建议你阅读不那么秘密的java代理系列教程(4部分)。这提供了很好的Java代理功能概述。

Hope this helps

希望这可以帮助

#1


3  

It sounds more like a packaging issue. In general you do it right: You have to specify the following in your manifest.

这听起来更像是一个包装问题。通常,您可以正确执行此操作:您必须在清单中指定以下内容。

Premain-Class: testinstrumentation.TestAgent

Of course, the testinstrumentation.TestAgent class file should reside in the same jar. From your stacktrace I see that it looks in src.testinstrumentation however your code is supposed to be put into testinstrumentation package

当然,testinstrumentation.TestAgent类文件应该驻留在同一个jar中。从你的堆栈跟踪我看到它在src.testinstrumentation中看起来然而你的代码应该被放入testinstrumentation包中

I recommend you to read the Not so secret java agents series of tutorials (4 parts). This provides a pretty good overview of capabilities of java agents.

我建议你阅读不那么秘密的java代理系列教程(4部分)。这提供了很好的Java代理功能概述。

Hope this helps

希望这可以帮助