mercredi 9 avril 2014

Script de fourmi dans Eclipse utilise la mauvaise version de Java ? -Débordement de pile


I'm working on an older project that requires me to use Java 1.5, but when I run the build script, I get the error:


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

I believe this error refers to a newer version of Java being used even though I only want to use Java 1.5. I've altered the settings under Windows->Preferences->Java->Installed JREs and under Run As->External Tool Configuration->JRE such that both are pointing to Java 1.5. Under Run As->External Tool Configuration->Environment, I added variables JAVA_HOME and JRE_HOME pointing to the older version of Java. When adding this line to the build script:


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

Eclipse returns this value:


[echo] ant.java.version: 1.5

but the above error is still present. The only way I have been able to get the project to build is by altering my JAVA_HOME and JRE_HOME environment variables to point to the older versions of Java. Are there any other settings in Eclipse which I can change to build the project without changing my system environment variables? (i.e. Ant builds the project using Java 1.5 but the system variables still point at Java 1.7).


The code for 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>




There might be multiple causes:



  • you use a library that was compiled for java 1.6

  • inside the ant script, you can specify the java version you want to compile to. It might be a different version, than the version, that ant itself runs in.

  • inside the eclipse settings, you can select an ant installation. The ant installation might be compiled for java 1.6 (not very propable). You might add(?)/select another ant installation

  • if ant does not compile your source but uses the class files, that the eclipse autocompilation already produced, you need to change the project's java compiler version and the projects JRE in the project settings (obviously there ;-)




When you create a new java project you can specify "Use a project specific JRE". Use JDK installed as Java/Installed JREs. After the project is created you cannot change the project JRE rather than do it manually. On the Project/Properties/Java Compiler use JDK compliance settings: generated .class files compatibility.




Go to the library with all the jars. If you can run a bash script, do this:


#!/bin/bash
for jarfile in `ls *.jar`
do
directory=${jarfile%.jar}
if [ -d $directory ]
then
rm -r $directory
fi

mkdir $directory
cd $directory
jar xf ../$jarfile
cd ..
for classfile in `find ./ -name *.class`
do
file $classfile | grep -v "version 46" | grep -v "version 47" | grep -v "version 48" | grep -v "version 49"
done
done

That should spit out any class that is not version 46 (Java 1.2), version 47 (Java 1.3), 48 (Java 1.4) or 49 (Java 1.5). The quartz jar has a class that's 45.3, which is Java 1.1. Maybe that's an issue? If not (Googling suggests JVM 1.5 can run 1.1 classes), then exclude "45.3" as well:


file $classfile | grep -v "version 45.3" | grep -v "version 46" | grep -v "version 47" | grep -v "version 48" | grep -v "version 49"


I'm working on an older project that requires me to use Java 1.5, but when I run the build script, I get the error:


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

I believe this error refers to a newer version of Java being used even though I only want to use Java 1.5. I've altered the settings under Windows->Preferences->Java->Installed JREs and under Run As->External Tool Configuration->JRE such that both are pointing to Java 1.5. Under Run As->External Tool Configuration->Environment, I added variables JAVA_HOME and JRE_HOME pointing to the older version of Java. When adding this line to the build script:


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

Eclipse returns this value:


[echo] ant.java.version: 1.5

but the above error is still present. The only way I have been able to get the project to build is by altering my JAVA_HOME and JRE_HOME environment variables to point to the older versions of Java. Are there any other settings in Eclipse which I can change to build the project without changing my system environment variables? (i.e. Ant builds the project using Java 1.5 but the system variables still point at Java 1.7).


The code for 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>



There might be multiple causes:



  • you use a library that was compiled for java 1.6

  • inside the ant script, you can specify the java version you want to compile to. It might be a different version, than the version, that ant itself runs in.

  • inside the eclipse settings, you can select an ant installation. The ant installation might be compiled for java 1.6 (not very propable). You might add(?)/select another ant installation

  • if ant does not compile your source but uses the class files, that the eclipse autocompilation already produced, you need to change the project's java compiler version and the projects JRE in the project settings (obviously there ;-)



When you create a new java project you can specify "Use a project specific JRE". Use JDK installed as Java/Installed JREs. After the project is created you cannot change the project JRE rather than do it manually. On the Project/Properties/Java Compiler use JDK compliance settings: generated .class files compatibility.



Go to the library with all the jars. If you can run a bash script, do this:


#!/bin/bash
for jarfile in `ls *.jar`
do
directory=${jarfile%.jar}
if [ -d $directory ]
then
rm -r $directory
fi

mkdir $directory
cd $directory
jar xf ../$jarfile
cd ..
for classfile in `find ./ -name *.class`
do
file $classfile | grep -v "version 46" | grep -v "version 47" | grep -v "version 48" | grep -v "version 49"
done
done

That should spit out any class that is not version 46 (Java 1.2), version 47 (Java 1.3), 48 (Java 1.4) or 49 (Java 1.5). The quartz jar has a class that's 45.3, which is Java 1.1. Maybe that's an issue? If not (Googling suggests JVM 1.5 can run 1.1 classes), then exclude "45.3" as well:


file $classfile | grep -v "version 45.3" | grep -v "version 46" | grep -v "version 47" | grep -v "version 48" | grep -v "version 49"

0 commentaires:

Enregistrer un commentaire