aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
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
Author

Unable to compile java file through ant

Graham Thorpe
Ranch Hand

Joined: Mar 25, 2002
Posts: 265
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Carol Enderlin
drifter
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.
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

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: http://aspose.com/file-tools
 
subject: Unable to compile java file through ant