Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to compile java file through ant

 
Graham Thorpe
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi


I am getting the following exception while executing the ant script


Buildfile: build.xml
[echo] Compiling java file................
[echo] Closing java file................
[echo] Building MySrc.jar................
[echo] Closing MySrc.jar................

compile:
[javac] Compiling 1 source file to C:\AntBuild\ant\reddy
[javac] [parsing started C:\AntBuild\ant\reddy\Helloo.java]
[javac] [parsing completed 140ms]
[javac] [loading C:\Program Files\Java\jre1.5.0\lib\rt.jar(java/lang/Object.
class)]
[javac] C:\AntBuild\ant\reddy\Helloo.java:3: cannot access java.lang.Object
[javac] bad class file: C:\Program Files\Java\jre1.5.0\lib\rt.jar(java/lang/
Object.class)
[javac] class file has wrong version 49.0, should be 48.0
[javac] Please remove or make sure it appears in the correct subdirectory of
the classpath.
[javac] public static void main(String ll[])
[javac] ^
[javac] [total 5367ms]
[javac] 1 error

BUILD FAILED



I am enclosing the build.xml file too


Build.xml
-----------

<project name="MyTask" basedir="." default="compile">

<target name="clean" description="Delete all generated files">
<delete dir="classes"/>
<delete file="MySrc.jar"/>
</target>
<property name="debug.flag" value="off"/>


<echo>Compiling java file................</echo>

<target name="compile" description="Compiles the Task">
<javac srcdir="reddy" destdir="reddy" includes="**/*.java" verbose="true">
</javac>
</target>
<echo>Closing java file................</echo>

<echo>Building MySrc.jar................</echo>
<target name="jar" description="JARs the Task">
<jar destfile="MySrc.jar" basedir="reddy"/>
</target>
<echo>Closing MySrc.jar................</echo>
</project>






Can any one pls clarify the above error ...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're asking a JDK 1.4 compiler to compile against the JDK 1.5 runtime classes, and it's refusing to do so. Check your CLASSPATH environment variable (which shouldn't include any copy of rt.jar) and check your build.properties file, if you have one.
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see two issues in a quick look at your errors.

1. [loading C:\Program Files\Java\jre1.5.0\lib\rt.jar(java/lang/Object.
class)]

This is loading jar from the JRE (java runtime) instead of the jdk (includes tools).


2. [javac] class file has wrong version 49.0, should be 48.0

This complaint is JDK 1.4 (class version 48) indicating it is finding JDK1.5 classes (class version 49).
 
Kaspar Christenson
Greenhorn
Posts: 21
Mac Scala VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was bit by this for the second time in four year, but had forgotten the cause and fix.
The fix for Maven and Ant is to specify the source and target version.

For Ant it would be <javac ... source="1.4" target="1.4">

The Maven solution is left as an exercise for the student.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic