[英]strange exception of “java.lang.NoClassDefFoundError: javassist/ClassPath” when running with commandline
I hava a agent class, it simply print two lines before and after another project's main() method.我有一个代理 class,它只是在另一个项目的 main() 方法之前和之后打印两行。 The agent class is as follows
代理class如下
public class Agent {
static String packageName = "xxx.hello.world";
public static void premain(String agentArgs, Instrumentation inst) {
inst.addTransformer((classLoader, s, aClass, protectionDomain, bytes) -> {
byte[] transformed = null;
CtClass cl = null;
try {
ClassPool pool = new ClassPool();
pool.insertClassPath(new LoaderClassPath(Thread.currentThread().getContextClassLoader()));
pool.importPackage(packageName);
cl = pool.makeClass(new ByteArrayInputStream(bytes));
CtMethod[] methods = cl.getDeclaredMethods();
for (CtMethod method : methods) {
if ("main".equals(method.getName())) {
method.insertBefore("System.out.println(\"<-----------before------->\");");
method.insertAfter("System.out.println(\"<-----------end------->\");");
}
}
transformed = cl.toBytecode();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cl != null) {
cl.detach();
}
}
return transformed;
});
}}
And I have another project named as "HelloWorld", it simply print "Hi, i am in main()" in it's main method.我还有另一个名为“HelloWorld”的项目,它只是在它的主要方法中打印“嗨,我在 main()”。 The HelloWorld.class is as follows
HelloWorld.class如下
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hi, i am in main()");
}
Then I package the agent as a jar named as "agent.jar", and add "-javaagent:xxx/yyy/agent.jar" to vmoptions in IDEA, like this然后我将 package 代理作为 jar 命名为“agent.jar”,并将“-javaagent:xxx/yyy/agent.jar”添加到 IDEA 中的 vmoptions,就像这样
After that, when I run the hello-world, I get what i want like之后,当我运行 hello-world 时,我得到了我想要的
<-----------before------->
Hi, i am in main()
<-----------end------->
However, when I try to run the hello-world as a jar like "hello-world.jar" in command line like但是,当我尝试将 hello-world 作为 jar 运行时,例如命令行中的“hello-world.jar”
java -javaagent:xxx/yyy/agent.jar -jar hello-world.jar
I get a strange error like我收到一个奇怪的错误,例如
Exception in thread "main" java.lang.NoClassDefFoundError: javassist/ClassPath
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getDeclaredMethod(Unknown Source)
at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(Unknown Source)
at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javassist.ClassPath
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 5 more
It is quite strange because I think the two ways (in IDEA and in command line) are the same.这很奇怪,因为我认为这两种方式(在 IDEA 和命令行中)是相同的。 And when i check the hello-world.jar, i can see the javassist/ClassPath.class.
当我检查 hello-world.jar 时,我可以看到 javassist/ClassPath.class。
Can any tell me why and how to fix this problem?谁能告诉我为什么以及如何解决这个问题?
Thanks!谢谢!
I faced the same issue, you can refer to the pom.xml below, and see the difference with yours in hello-world project.我遇到了同样的问题,你可以参考下面的 pom.xml,看看你在 hello-world 项目中的不同之处。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.richard</groupId>
<artifactId>my-agent-test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.richard.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.20.0-GA</version>
</dependency>
</dependencies>
</project>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.