我正在创建一个java代理,它将用于对某些类进行字节码修改,org.eclipse.jdt.core.JDTCompilerAdapter
就是其中之一。我正在使用javassit修改org.eclipse.jdt.core.JDTCompilerAdapter
的一些org.eclipse.jdt.core.JDTCompilerAdapter
方法。因此,我在代理项目中包含了ecj (使用gradle)。
compile group: 'org.eclipse.jdt.core.compiler' ,name: 'ecj', version :'4.3.1'
因为我需要使用ecj的一些类。
代理的目标是拦截要执行方法的调用,修改execute方法以向我的类添加一些调用,以触发某些处理。
我正在用带有两个类的简单java项目来测试代理。该项目使用ant构建,并使用JDTCompilerAdapter
作为编译器。
这是build.xml文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="build" name="TestProject">
<property file="build.properties" />
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.7"/>
<property name="source" value="1.7"/>
<path id="PClasspath">
<pathelement location="bin"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="bin"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="init" name="build">
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="PClasspath"/>
</javac>
</target>
<!--
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
<copy todir="${ant.library.dir}">
<fileset dir="${ECLIPSE_JDT_CORE}" includes="*.jar"/>
</copy>
</target>-->
<target name="build-e" >
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
在构建项目时将使用代理。因此,为了测试代理,我使用以下命令:
java -jar agent-wrapper.jar --outdir ./out --exec ./build_wrapper.sh
build_wrapper.sh包含这样的内容(我添加了ecj依赖项,这样我就可以像在bulid.xml <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
中那样用JDTCompilerAdapter
编译该项目:
../ant/bin/ant -lib ../eclipse/plugins/ecj-4.3.1.jar build-e
其思想是,代理包装器将解析参数(outdir用于生成一些内容,exec是一个用于启动测试项目构建的脚本),获取要从build_wrapper.sh
执行的命令(在本例中为../ant/bin/ant -lib ../eclipse/plugins/ecj-4.3.1.jar build-e
),并将其作为java代理添加到命令中。
此问题发生在代理的执行过程中。这是输出:
java -jar custom-agent.jar --outdir ./out --exec ./build_wrapper.sh [10:18:53]
Picked up JAVA_TOOL_OPTIONS: -javaagent:/Users/dev/TestAgent/project/custom-agent.jar=OUTDIR=/Users/dev/TestAgent/project/./out
objc[30474]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/bin/java and /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
Buildfile: /Users/dev/TestAgent/project/build.xml
build-e:
init:
[mkdir] Created dir: /Users/dev/TestAgent/project/bin
build:
BUILD FAILED
/Users/dev/TestAgent/project/build.xml:47: The following error occurred while executing this line:
/Users/dev/TestAgent/project/build.xml:32: Class org.eclipse.jdt.core.JDTCompilerAdapter could not be loaded because of an invalid dependency.
Total time: 2 seconds
abnormal termination, exit code: 1
当我在代理项目中不使用ecj-4.3.1.jar时,构建运行良好--我拦截对execute()
方法的调用,但不能使用ecj中的其他类。
发布于 2015-11-18 17:03:10
显示停止错误是“由于无效的依赖关系而无法加载类org.eclipse.jdt.core.JDTCompilerAdapter”。
从读取此链接adapter.htm中可以找到错误的第一个提示。
第二个提示可能是缺少运行JDTCompilerAdapter所需的一个jars。
为了使JDTCompilerAdapter正常工作,我将JDTCompilerAdapter.jar和org.eclipse.jdt.core.jar复制到ant/lib文件夹中。
在上面提到的链接中,基于eclipse版本和java版本的差异是有记录的。
https://stackoverflow.com/questions/33599163
复制相似问题