JAXWS-RI WSGEN 配置使用特定 JDK

JAXWS-RI WSGEN configure to use specific JDK

我目前正在尝试使用 ANT 在 CruiseControl 中编译应用程序,但得到了 UnsupportedClassVersionError。设置如下:

CruiseControl 作为一项服务安装,在启动期间 JAVA_HOME 在名为 wrapper.conf 的文件中设置为 JDK 6。由于目标版本是 Java 8,还设置了另一个环境变量。

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:\path\build.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[=13=]0(URLClassLoader.java:56)
    at java.net.URLClassLoader.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(指向 JDK 6)来查找随后用于生成内容的 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 配置为在编译时使用 JDK 8 而不是 JDK 6,但到目前为止我无法这样做。有谁知道我如何解决这个问题?我应该提一下,目前无法在 JDK8 上将 CruiseControl 升级到 运行。

错误有以下...

Caused by: java.lang.UnsupportedClassVersionError: com/company/area/thing/services/Service1 : Unsupported major.minor version 52.0

/*snip*/

at java.lang.ClassLoader.loadClass(ClassLoader.java:251)

at com.sun.tools.ws.wscompile.WsgenOptions.getClass(WsgenOptions.java:305)

错误是说 Java 运行时(JRE)运行 Ant 的版本早于 JRE 52.0 版。 JRE 52.0 是 Java SE 8 的主要编号(参见 How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version)。

JRE 运行 Ant 正在尝试与 Java class com/company/area/thing/services/Service1 进行交互。但是,Service1 class 是用 Java 8 编译的。较旧的 JRE 运行 Ant 不知道如何处理 Java 8 class es,所以构建失败。

使用 Java 尝试 运行 Ant 8. 将 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