我正在尝试用java代理编写一个示例java字节码检测。我正在使用正常命令行并使用maven。
这是我的文件夹结构:
--- bin
--- lib ---> /javassist-3.22.0-GA
--- META_INF --> MANIFEST.MF
--- src -- > org/springframework/aop/framework/ .java files
--- .classpath
--- .project
MANIFEST.MF文件包含:
Manifest-Version: 1.0
Built-By: Cycle
Build-Jdk: 1.8.0_121
premain-class: org.springframework.aop.framework.DurationAgent
Created-By: vkp
在eclipse .classpath
中包含以下库
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="C:/Users/kp/javassist-3.22.0-GA.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="D:/vkp/antlr-2.7.7.jar"/>
<classpathentry kind="lib" path="D:/vkp/aopalliance-1.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/bcpkix-jdk15on-1.48.jar"/>
<classpathentry kind="lib" path="D:/vkp/bcprov-jdk15on-1.48.jar"/>
<classpathentry kind="lib" path="D:/vkp/bcprov-jdk16-1.46.jar"/>
<classpathentry kind="lib" path="D:/vkp/cglib-nodep-2.2.jar"/>
<classpathentry kind="lib" path="D:/vkp/com.springsource.com.ibm.websphere.uow-6.0.2.17.jar"/>
<classpathentry kind="lib" path="D:/vkp/com.springsource.commonj-1.1.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/commons-codec-1.4.jar"/>
<classpathentry kind="lib" path="D:/vkp/commons-collections-3.2.jar"/>
<classpathentry kind="lib" path="D:/vkp/commons-fileupload-1.2.2.jar"/>
<classpathentry kind="lib" path="D:/vkp/commons-httpclient-3.1.jar"/>
<classpathentry kind="lib" path="D:/vkp/commons-io-1.4.jar"/>
<classpathentry kind="lib" path="D:/vkp/commons-lang-2.5.jar"/>
<classpathentry kind="lib" path="D:/vkp/commons-net-3.1.jar"/>
<classpathentry kind="lib" path="D:/vkp/db2jcc_license_cisuz-4.8.87.jar"/>
<classpathentry kind="lib" path="D:/vkp/db2jcc_license_cu-4.8.87.jar"/>
<classpathentry kind="lib" path="D:/vkp/db2jcc4-4.8.87.jar"/>
<classpathentry kind="lib" path="D:/vkp/dom4j-1.6.1.jar"/>
<classpathentry kind="lib" path="D:/vkp/easymock-3.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/freemarker-2.3.16.jar"/>
<classpathentry kind="lib" path="D:/vkp/grammatica-1.4.jar"/>
<classpathentry kind="lib" path="D:/vkp/gson-2.3.1.jar"/>
<classpathentry kind="lib" path="D:/vkp/hibernate-annotations-3.5.6-Final.jar"/>
<classpathentry kind="lib" path="D:/vkp/hibernate-commons-annotations-3.2.0.Final.jar"/>
<classpathentry kind="lib" path="D:/vkp/hibernate-core-3.5.6-Final.jar"/>
<classpathentry kind="lib" path="D:/vkp/hibernate-ehcache-3.3.2.GA.jar"/>
<classpathentry kind="lib" path="D:/vkp/hibernate-entitymanager-3.4.0.GA.jar"/>
<classpathentry kind="lib" path="D:/vkp/hibernate-jpa-2.0-api-1.0.0.Final.jar"/>
<classpathentry kind="lib" path="D:/vkp/httpclient-4.2.5.jar"/>
<classpathentry kind="lib" path="D:/vkp/httpcore-4.2.5.jar"/>
<classpathentry kind="lib" path="D:/vkp/ibmjceprovider-8.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/ibmpkcs-8.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/imaging-01012005.jar"/>
<classpathentry kind="lib" path="D:/vkp/javax.persistence-2.0.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/javolution-5.5.1.jar"/>
<classpathentry kind="lib" path="D:/vkp/jcaptcha-1.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/jcaptcha-api-1.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/jcifs-1.2.6.jar"/>
<classpathentry kind="lib" path="D:/vkp/jcl-over-slf4j-1.6.1.jar"/>
<classpathentry kind="lib" path="D:/vkp/jcommon-1.0.15.jar"/>
<classpathentry kind="lib" path="D:/vkp/jetty-6.1.26.jar"/>
<classpathentry kind="lib" path="D:/vkp/jetty-client-6.1.26.jar"/>
<classpathentry kind="lib" path="D:/vkp/jetty-embedded-6.1.26.jar"/>
<classpathentry kind="lib" path="D:/vkp/jetty-util-6.1.26.jar"/>
<classpathentry kind="lib" path="D:/vkp/jfreechart-1.0.13.jar"/>
<classpathentry kind="lib" path="D:/vkp/jradius-client-20040211.jar"/>
<classpathentry kind="lib" path="D:/vkp/jta-1.1.jar"/>
<classpathentry kind="lib" path="D:/vkp/jtidy-r8-20060801.jar"/>
<classpathentry kind="lib" path="D:/vkp/junit-4.8.2.jar"/>
<classpathentry kind="lib" path="D:/vkp/log4j-1.2.14.jar"/>
<classpathentry kind="lib" path="D:/vkp/mail-1.4.1.jar"/>
<classpathentry kind="lib" path="D:/vkp/mibble-2.8.jar"/>
<classpathentry kind="lib" path="D:/vkp/mibble-mibs-2.8.jar"/>
<classpathentry kind="lib" path="D:/vkp/mockito-all-1.9.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/poi-3.2-FINAL.jar"/>
<classpathentry kind="lib" path="D:/vkp/selenium-android-driver-2.31.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/selenium-api-2.31.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/selenium-chrome-driver-2.31.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/selenium-firefox-driver-2.31.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/selenium-htmlunit-driver-2.31.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/selenium-ie-driver-2.31.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/selenium-iphone-driver-2.31.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/selenium-java-2.31.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/selenium-remote-driver-2.31.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/selenium-safari-driver-2.31.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/selenium-server-2.31.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/selenium-support-2.31.0.jar"/>
<classpathentry kind="lib" path="D:/vkp/servlet-api-2.5.jar"/>
<classpathentry kind="lib" path="D:/vkp/slf4j-api-1.6.1.jar"/>
<classpathentry kind="lib" path="D:/vkp/slf4j-log4j12-1.6.1.jar"/>
<classpathentry kind="lib" path="D:/vkp/snmp4j-2.0.3.jar"/>
<classpathentry kind="lib" path="D:/vkp/snmp4j-agent-2.0.5.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-aop-3.0.7.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-asm-3.0.7.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-beans-3.0.7.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-context-3.0.7.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-context-support-3.0.7.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-core-3.0.7.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-expression-3.0.7.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-jdbc-3.0.7.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-ldap-core-1.3.1.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-orm-3.0.7.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-oxm-3.1.3.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-security-acl-3.1.3.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-security-config-3.1.3.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-security-core-3.1.3.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-security-ldap-3.1.3.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-security-taglibs-3.1.3.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-security-web-3.1.3.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-test-3.0.7.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-tx-3.0.7.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-web-3.0.7.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-webmvc-3.0.7.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-ws-core-2.1.2.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-ws-security-2.1.2.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/spring-xml-2.1.2.RELEASE.jar"/>
<classpathentry kind="lib" path="D:/vkp/wsdl4j-1.6.1.jar"/>
<classpathentry kind="lib" path="D:/vkp/wss4j-1.6.5.jar"/>
<classpathentry kind="lib" path="D:/vkp/xmlschema-core-2.0.3.jar"/>
<classpathentry kind="lib" path="D:/vkp/xmlsec-1.5.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
DurationAgent.java文件
package org.springframework.aop.framework;
import java.lang.instrument.Instrumentation;
public class DurationAgent {
// for all the class loaded, premain will be called
public static void premain(String agentArgs, Instrumentation inst) {
System.out.println("Executing premain.........");
inst.addTransformer(new JavassistApplicationContext());
}
}
JavassistApplicationContext.java文件
package org.springframework.aop.framework;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.LoaderClassPath;
import org.springframework.web.context.support.XmlWebApplicationContext;
import java.io.ByteArrayInputStream;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
public class JavassistApplicationContext extends XmlWebApplicationContext implements ClassFileTransformer {
public byte[] transform(ClassLoader loader, String className,
Class classBeingRedefined, ProtectionDomain protectionDomain,
byte[] classfileBuffer) throws IllegalClassFormatException {
byte[] byteCode = classfileBuffer;
System.out.println("Instrumenting......");
ClassPool classPool = ClassPool.getDefault();
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
classPool.appendClassPath(new LoaderClassPath(classLoader));
CtClass cc = classPool.get("org.springframework.aop.framework.ProxyCreatorSupport");
CtConstructor c = cc.getConstructors()[0];
c.insertAfter("$0.aopProxyFactory = new org.springframework.aop.framework.JavassistAopProxyFactory();");
//cc.toClass();
byteCode = cc.toBytecode();
cc.detach();
System.out.println("Instrumentation complete.");
} catch (Exception e) {
System.out.println("Exception: " + e);
e.printStackTrace();
throw new IllegalStateException(e);
}
return byteCode;
}
}
我用eclipse构建,我得到了类文件。之后我用命令jar cvfm instrumentation.jar ..\meta-inf\manifest.mf .
现在我使用带有java代理的javassit库运行该文件
java -cp .;/lib/javassist-3.22.0-GA.jar; -javaagent:./bin/instrumentation.jar org.springframework.aop.framework.TestInstrumentation
我也试过
java -cp .;/lib/javassist-3.22.0-GA.jar;/lib/spring-web-3.0.7.RELEASE.jar; -javaagent:./bin/instrumentation.jar org.springframework.aop.framework.TestInstrumentation
但我仍然得到例外。
Executing premain.........
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(Unknown Source)
at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: org/springframework/web/context/support/XmlWebApplication
Context
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
at org.springframework.aop.framework.DurationAgent.premain(DurationAgent.java:10)
... 6 more
Caused by: java.lang.ClassNotFoundException: org.springframework.web.context.support.XmlWebApplicati
onContext
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)
... 19 more
FATAL ERROR in native method: processing of -javaagent failed
有什么建议吗?我做错了什么?请帮忙
从代码中删除扩展XmlWebApplicationContext并使用
运行java -cp .;/lib/javassist-3.22.0-GA.jar -javaagent:./bin/instrumentation.jar org.springframework.aop.framework.TestInstrumentation
在javassit上获取错误:
Executing premain.........
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(Unknown Source)
at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: javassist/ClassPath
at org.springframework.aop.framework.DurationAgent.premain(DurationAgent.java:10)
... 6 more
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)
... 7 more
FATAL ERROR in native method: processing of -javaagent failed