views:

96

answers:

5

I have installed Eclipse 3.5.2 and the JDK for Java 6.

Here's my installed JREs in Eclipse

alt text

I am trying to compile with an ant build file, part of which looks like this and specifies java 1.5:

<target name="compile" depends="build-common, init" description="Compile files. ">
    <javac srcdir="${src_dir}" destdir="${build_dir}" debug="true" target="1.5" source="1.5">
        <classpath path="${tomcat_home}/lib/servlet-api.jar;${tomcat_home}/lib/log4j-1.2.15.jar;/usr/local/lib/portlet-api-1.0.jar;." />
    </javac>
</target>

But when I try to compile, the console window displays the following error:

compile:
    [javac] Compiling 1 source file to H:\jephperro\portlets\build
    [javac] javac: invalid target release: 1.5
    [javac] Usage: javac <options> <source files>
    [javac] where possible options include:
    [javac]   -g                        Generate all debugging info
    [javac]   -g:none                   Generate no debugging info
    [javac]   -g:{lines,vars,source}    Generate only some debugging info
    [javac]   -nowarn                   Generate no warnings
    [javac]   -verbose    ....

BUILD FAILED
H:\jephperro\portlets\CourseList-build.xml:25: Compile failed; see the compiler error output for details.

Total time: 531 milliseconds

What's my problem with Eclipse?

A: 

see the compiler error output for details.

You probably have a dependency on a library that was compiled using a later version of Java than your 1.5 JDK.

Actually, where is your 1.5 JDK? All I see is a JRE. My guess is that you just need to download a version 1.5 JDK and add that in Eclipse.

Bill the Lizard
There's a couple of JDKs in that "installed JRE" list. I'm suspecting that you've put your finger on the problem, though.
Carl Smotricz
You don't need a 1.5 JDK to use -target 1.5
Michael Borgwardt
Why the heck would I need to install 1.5 JDK when I have 1.6 installed?
jeph perro
@Michael: Ah, that's right. -source 1.5 shouldn't hurt anything either, so that takes me back to thinking it's a dependency that we can't see.
Bill the Lizard
A: 

You could create a task in your ant build file that runs the equivalent of java -version so you'll get an idea of which Java compiler is being used by the ant that's started up by Eclipse.

Hint: Your default JRE is a 1.6 JRE. That's fine for running code, but not for compiling. Only a JDK contains the magic required by an external compile (such as done by ant). Eclipse gets around this by including an incremental Java compiler in its own code (more magic).

After years, I still don't fully understand how Eclipse, ant and the JDK interoperate, so maybe you need to do a little experimenting.

Carl Smotricz
A: 

AFAIR Eclipse does not use its own internal Java compiler when an Ant file is run. Check your local paths and try to find out which javac is called by Ant.

vwegert
A: 

The 'javac: invalid target release: 1.5' Compilation error is commonly caused by source/binary level incompatibility. Meaning you are trying to compile a source level of JDK 5 with a JDK 1.4 or less.

Eclipse uses a built-in Java compiler. The level actually followed by the compiler depends on the project settings. You can configure the Java level per project or set it as a default at a global level.

From the menu bar, select Window->Preferences. Select the Java->Compiler preference. Set the Compiler Compliance level to 5.0.

Saifuddin
A: 

As Saifuddin and others mentioned this error is most likely the cause of not using the right Java compiler for the version you want. I notice in your installed JRE's there is a JDK located in DevsuiteHome_1, doesn't say what version. Maybe Ant is using that?

It is very easy to check. You are running ant within Eclipse. Ant has it's own configuration settings that can be different to your workspace. To check the version Ant is using when it runs follow these steps:

Run -> External Tools -> External Tools Configuration -> click on your ant build file (should be created if you ran it once already if not you could always create it here) -> select the JRE Tab -> Verify the runtime you are using

Ross