File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Ant, Maven and Other Build Tools and the fly likes Unable to compile java file through ant Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "Unable to compile java file through ant" Watch "Unable to compile java file through ant" New topic

Unable to compile java file through ant

Graham Thorpe
Ranch Hand

Joined: Mar 25, 2002
Posts: 265

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................

[javac] Compiling 1 source file to C:\AntBuild\ant\reddy
[javac] [parsing started C:\AntBuild\ant\reddy\]
[javac] [parsing completed 140ms]
[javac] [loading C:\Program Files\Java\jre1.5.0\lib\rt.jar(java/lang/Object.
[javac] C:\AntBuild\ant\reddy\ cannot access java.lang.Object
[javac] bad class file: C:\Program Files\Java\jre1.5.0\lib\rt.jar(java/lang/
[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


I am enclosing the build.xml file too


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

<target name="clean" description="Delete all generated files">
<delete dir="classes"/>
<delete file="MySrc.jar"/>
<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">
<echo>Closing java file................</echo>

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

Can any one pls clarify the above error ...
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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 file, if you have one.

[Jess in Action][AskingGoodQuestions]
Carol Enderlin
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
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.

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

Joined: Feb 26, 2009
Posts: 21

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.
I agree. Here's the link:
subject: Unable to compile java file through ant
It's not a secret anymore!