gpt4 book ai didi

java - Eclipse 中的 Ant 脚本使用了不正确的 Java 版本

转载 作者:行者123 更新时间:2023-12-02 07:38:38 25 4
gpt4 key购买 nike

我正在开发一个需要使用 Java 1.5 的旧项目,但是当我运行构建脚本时,出现错误:

java.lang.UnsupportedClassVersionError: Bad version number in .class file

我相信此错误是指正在使用较新版本的 Java,即使我只想使用 Java 1.5。我更改了 Windows->Preferences->Java->Installed JREsRun As->External Tool Configuration->JRE 下的设置,以便两者都指向Java 1.5。在运行方式->外部工具配置->环境下,我添加了指向旧版本Java的变量JAVA_HOMEJRE_HOME。将此行添加到构建脚本时:

<echo message="ant.java.version: ${ant.java.version}"/>

Eclipse 返回此值:

[echo] ant.java.version: 1.5

但是上面的错误仍然存​​在。我能够构建项目的唯一方法是更改​​我的 JAVA_HOMEJRE_HOME 环境变量以指向旧版本的 Java。我可以更改 Eclipse 中的任何其他设置来构建项目,而无需更改系统环境变量吗? (即 Ant 使用 Java 1.5 构建项目,但系统变量仍然指向 Java 1.7)。

build.xml的代码:

<project name="osd-bits" basedir="." default="deploy">

<!-- Project settings -->

<!-- TODO -->
<!-- This needs to be set individually by each developer to the correct Tomcat path -->
<property name="tomcat.path" value="C:\Tomcat 6\apache-tomcat-6.0.35" />

<property name="project.title" value="OSD BITS"/>
<property name="project.name" value="osd-bits"/>
<property name="project.version" value="2.0"/>
<property name="project.deploypath" value="${tomcat.path}\\webapps"/>
<property name="project.workdir" value="${tomcat.path}\\work\Catalina\localhost"/>

<!-- Test Server Settings -->
<property name="project.deploypathtest" value="T:\Program Files\jakarta-tomcat-5.0.28\webapps"/>
<property name="project.workdirtest" value="T:\Program Files\jakarta-tomcat-5.0.28\work\Catalina\localhost"/>


<!-- Staging Server Settings -->
<property name="project.deploypathstage" value="S:\Program Files\jakarta-tomcat-5.0.28\webapps"/>
<property name="project.workdirstage" value="S:\Program Files\jakarta-tomcat-5.0.28\work\Catalina\localhost"/>

<!-- Production Server Settings -->
<property name="project.deploypathprod" value="P:\Program Files\jakarta-tomcat-5.0.28\webapps"/>
<property name="project.workdirprod" value="P:\Program Files\jakarta-tomcat-5.0.28\work\Catalina\localhost"/>

<!-- Path settings -->
<property name="path.binary" value="${basedir}/../../baja2/bin"/>
<property name="path.webroot" value="${basedir}/webroot"/>
<property name="path.conf" value="${basedir}/conf"/>
<property name="path.src" value="${basedir}/src"/>
<property name="path.build" value="${basedir}/bin"/>
<property name="path.cmdlib" value="${basedir}/cmd/lib"/>
<property name="path.buildlib" value="${tomcat.path}\\lib"/>
<property name="path.webinf" value="${path.webroot}/WEB-INF"/>
<property name="path.lib" value="${path.webinf}/lib"/>
<property name="path.classes" value="${path.webinf}/classes"/>
<property name="path.classpath" value="${path.lib}/baja.jar;
${path.lib}/struts.jar;
${path.buildlib}/fop.jar;
${basedir}/lib/servlet.jar;
${path.buildlib}/velocity-dep-1.4.jar;
${basedir}/lib/jxl.jar;
${path.lib}/display.jar;
${path.buildlib}/commons-validator.jar;
${path.buildlib}/commons-beanutils.jar;
${path.lib}/quartz-1.6.3.jar"/>

<!-- Check timestamp on files -->
<target name="timecheck">
<tstamp/>
</target>

<!-- Remove classes for clean build -->
<target name="clean" description="Prepare for clean build">
<delete dir="${path.build}/com"/>
<delete file="${path.lib}/${project.name}.jar"/>
<delete file="${path.lib}/${project.name}-config.jar"/>
</target>


<!-- jar up config files -->
<target name="config">
<jar jarfile="${path.lib}/${project.name}-config.jar" basedir="${path.conf}"/>
</target>

<!-- Full build of java and config files-->
<target name="build" depends="timecheck,clean,config">
<echo message="ant.java.version: ${ant.java.version}" />
<javac srcdir="${path.src}" destdir="${path.build}" classpath="${path.classpath}" debug="true" target="1.5" includeantruntime="false"/>
<jar jarfile="${path.lib}/${project.name}.jar" basedir="${path.build}"/>
</target>

<!-- Builds everything in src directory and deploys .war to deployment directory -->
<target name="deploy" depends="build" description="Deploys project to deployment directory">
<delete file="${project.deploypath}\${project.name}.war"/>
<delete dir="${project.deploypath}\${project.name}"/>
<delete dir="${project.workdir}\${project.name}"/>
<copy todir="${project.deploypath}\${project.name}">
<fileset dir="${path.webroot}"/>
</copy>
</target>

<!-- Builds everything in src directory and deploys to test server deployment directory -->
<target name="deploytotest" depends="build" description="Deploys project to test deployment directory">
<delete file="${project.deploypathtest}\${project.name}.war"/>
<delete dir="${project.deploypathtest}\${project.name}"/>
<delete dir="${project.workdirtest}\${project.name}"/>
<copy todir="${project.deploypathtest}\${project.name}">
<fileset dir="${path.webroot}"/>
</copy>
</target>

<!-- Builds everything in src directory and deploys to staging server deployment directory -->
<target name="deploytostage" depends="build" description="Deploys project to stage deployment directory">
<delete file="${project.deploypathstage}\${project.name}.war"/>
<delete dir="${project.deploypathstage}\${project.name}"/>
<delete dir="${project.workdirstage}\${project.name}"/>
<copy todir="${project.deploypathstage}\${project.name}">
<fileset dir="${path.webroot}"/>
</copy>
</target>

<!-- Builds everything in src directory and deploys to production server deployment directory -->
<target name="deploytoprod" depends="build" description="Deploys project to production deployment directory">
<delete file="${project.deploypathprod}\${project.name}.war"/>
<delete dir="${project.deploypathprod}\${project.name}"/>
<delete dir="${project.workdirprod}\${project.name}"/>
<copy todir="${project.deploypathprod}\${project.name}">
<fileset dir="${path.webroot}"/>
</copy>
</target>

<target name="movejsps" description="Moves all JSP files without doing a complete rebuild">
<copy todir="${project.deploypath}\${project.name}\jsp" preservelastmodified="false" >
<fileset dir="${path.webroot}/jsp" />
</copy>
</target>

<target name="movejs" description="Moves all JS files without doing a complete rebuild">
<copy todir="${project.deploypath}\${project.name}\js" preservelastmodified="false" >
<fileset dir="${path.webroot}/js" />
</copy>
</target>

<target name="movecss" description="Moves all CSS files without doing a complete rebuild">
<copy todir="${project.deploypath}\${project.name}\css" preservelastmodified="false" >
<fileset dir="${path.webroot}/css" />
</copy>
</target>

<target name="movetemplates" description="Moves all Report Template files without doing a complete rebuild">
<copy todir="${project.deploypath}\${project.name}\templates" preservelastmodified="false" >
<fileset dir="${path.webroot}/templates" />
</copy>
</target>


<target name="copyjar" description="Moves latests baja.jar into demos webinf lib">
<copy file="${path.binary}/baja.jar" todir="${path.lib}" preservelastmodified="true" />
</target>

<!-- Builds the command line distribution -->
<target name="deploycmd" depends="build" description="Deploys project to test deployment directory">
<delete file="${path.cmdlib}\${project.name}.jar"/>
<delete file="${path.cmdlib}\${project.name}-config.jar"/>
<copy file="${path.lib}/${project.name}.jar" todir="${path.cmdlib}" preservelastmodified="true" />
<copy file="${path.lib}/${project.name}-config.jar" todir="${path.cmdlib}" preservelastmodified="true" />
</target>

最佳答案

可能有多种原因:

  • 您使用的是为 java 1.6 编译的库
  • 在ant脚本中,您可以指定要编译到的java版本。它可能是与 ant 本身运行的版本不同的版本。
  • 在 eclipse 设置中,您可以选择 ant 安装。 ant 安装可能是针对 java 1.6 编译的(不太可能)。您可以添加(?)/选择另一个 ant 安装
  • 如果 ant 不编译您的源代码,而是使用 Eclipse 自动编译已生成的类文件,您需要在项目设置中更改项目的 java 编译器版本和项目 JRE(显然在那里;-)

关于java - Eclipse 中的 Ant 脚本使用了不正确的 Java 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11869648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com