我目前正试图使用ANT在CruiseControl中编译一个应用程序,但收到了UnsupportedClassVersionError。设置如下:
- 环境:Windows服务器2012
- IDE:Eclipse Luna运行JDK 8
- 构建服务器:运行在JDK 6上的CruiseControl 2.4.1
- 构建工具:ANT
- 编译器:JDK 8更新102
- JAXWS-RI:2.2.10
CruiseControl是作为一项服务安装的,在启动期间,JAVA_HOME在名为wrapper.conf的文件中设置为JDK 6。由于目标版本是JAVA 8,因此还设置了另一个env变量。
set.JAVA_HOME=C:/jdk1.6
set.JAVA_HOME_JDK8=C:/jdk1.8.0_102
在ANT构建脚本中,javac任务将fork设置为true,并指定java可执行文件的路径,如下所示:
<property name="javac.fork" value="yes"/>
<property environment="env"/>
<property name="javac.location" value="${env.JAVA_HOME_JDK8}\bin\javac.exe"/>
<javac includeantruntime="false" destdir="${builddir}" debug="true"
target="1.8" source="1.8" debuglevel="lines,vars,source"
fork="${javac.fork}" executable="${javac.location}">
上面的设置运行良好,但当我尝试使用JAXWS-RI创建工件时失败了。报告的具体错误为:
java.lang.UnsupportedClassVersionError: com/company/area/thing/services/Service1 : Unsupported major.minor version 52.0
at org.apache.tools.ant.ProjectHelper.addLocationToBuildException(ProjectHelper.java:539)
at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:384)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at org.apache.tools.ant.Main.runBuild(Main.java:668)
at org.apache.tools.ant.Main.startAnt(Main.java:187)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)
Caused by: C:pathbuild.xml:133: The following error occurred while executing this line:
java.lang.UnsupportedClassVersionError: com/company/area/thing/services/Service1 : Unsupported major.minor version 52.0
at org.apache.tools.ant.ProjectHelper.addLocationToBuildException(ProjectHelper.java:539)
at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:384)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at org.apache.tools.ant.helper.SingleCheckExecutor.executeTargets(SingleCheckExecutor.java:37)
at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:382)
... 12 more
Caused by: java.lang.UnsupportedClassVersionError: com/company/area/thing/services/Service1 : Unsupported major.minor version 52.0
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1225)
at org.apache.tools.ant.helper.SingleCheckExecutor.executeTargets(SingleCheckExecutor.java:37)
at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:382)
... 20 more
Caused by: java.lang.UnsupportedClassVersionError: com/company/area/thing/services/Service1 : Unsupported major.minor version 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at com.sun.tools.ws.wscompile.WsgenOptions.getClass(WsgenOptions.java:305)
at com.sun.tools.ws.wscompile.WsgenOptions.validateEndpointClass(WsgenOptions.java:244)
at com.sun.tools.ws.wscompile.WsgenOptions.validate(WsgenOptions.java:235)
at com.sun.tools.ws.wscompile.WsgenTool.run(WsgenTool.java:131)
at com.sun.tools.ws.ant.WsGen2.runInVm(WsGen2.java:357)
at com.sun.tools.ws.ant.WsTask2.execute(WsTask2.java:423)
at com.sun.tools.ws.ant.WsGen2.execute(WsGen2.java:351)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
... 23 more
据我所见,它被抛出是因为WSGEN使用JAVA_HOME(指向JDK6)来查找随后用于生成内容的JAVA可执行文件。
WSSGEN设置为:
<target name="wsgen-compile">
<mkdir dir="${builddir}"/>
<javac includeantruntime="false" destdir="${builddir}" debug="true" target="1.8" source="1.8" debuglevel="lines,vars,source" fork="${javac.fork}" executable="${javac.location}">
<src path="${src.tmp}" />
<classpath>
<pathelement path="${classpath}"/>
<pathelement path="${builddir}/../../common/bin"/>
<pathelement path="${builddir}/../../server/bin"/>
<fileset dir="${libdir}">
<include name="*.jar"/>
</fileset>
</classpath>
</javac>
</target>
<target name="wsgen-generate">
<taskdef name="wsgen" classname="com.sun.tools.ws.ant.WsGen2">
<classpath>
<fileset dir="${webserviceslibdir}/jax/jaxws-ri/lib">
<include name="*.jar"/>
</fileset>
</classpath>
</taskdef>
<wsgen verbose="true"
genwsdl="true"
xendorsed="false"
keep="true"
resourcedestdir="properties"
destdir="${src.tmp}"
sei="com.nordea.nordiccms.server.webservices.${service}">
<classpath>
<pathelement path="${classpath}"/>
<pathelement path="${builddir}/../../common/bin"/>
<pathelement path="${builddir}/../../server/bin"/>
<fileset dir="${libdir}">
<include name="*.jar"/>
</fileset>
</classpath>
<arg value="J.java_home"/>
</wsgen>
<delete>
<fileset dir="${src.tmp}">
<include name="**/*.class"/>
</fileset>
</delete>
</target>
<target name="wsgen" depends="compile">
<echo message=""/>
<echo message="Building ${distributionnameserver} WebService files"/>
<echo message="----------"/>
<mkdir dir="${builddir}/wsgen"/>
<delete dir="${src.tmp}"/>
<mkdir dir="${src.tmp}"/>
<ant target="wsgen-generate">
<property name="service" value="Service1"/>
</ant>
<ant target="wsgen-compile"/>
<delete dir="${src.tmp}"/>
</target>
我想在编译时将WSGEN配置为使用JDK8而不是JDK6,但到目前为止我还无法做到。有人知道我该如何解决这个问题吗?我应该提到的是,在这一点上,升级CruiseControl以在JDK8上运行不是一个选项。
错误如下。。。
由java.lang.UnsupportedClassVersionError引起:com/company/area/thing/services/Service1:不支持的major.minor版本52.0
/*snip*/
在java.lang.ClassLoader.loadClass(ClassLoader.java:251)
网址:com.sun.tools.ws.wscompile.WsgenOptions.getClass(WsgenOptions.java:305)
错误表示运行Ant的Java Runtime(JRE)版本早于JRE 52.0版本。JRE 52.0是Java SE 8的主要版本号(请参阅如何修复Java.lang.UnsupportedClassVersionError:不支持的major.minor版本)。
运行Ant的JRE正试图与Java类com/company/area/thing/services/Service1
进行交互。然而,Service1
类是用Java 8编译的。运行Ant的旧JRE不知道如何处理Java8类,因此构建失败。
尝试使用Java 8运行Ant。将JAVA_HOME
设置为指向JDK 8的安装。
使用以下build.xml
脚本验证正在使用的Java版本:
<project name="ant-jdk-version">
<echo>Java Version via Ant: ${ant.java.version}</echo>
<echo>Java Version System Prop: ${java.version}</echo>
</project>
脚本应该输出类似于…的内容
[echo] Java Version via Ant: 1.8
[echo] Java Version System Prop: 1.8.0_60