tags:

views:

137

answers:

3

It seems like this should be a simple task, with the options in the Preferences menu for different JREs and the ability to set different compiler and build paths per project. However, it also seems to simply not work.

For example, I have my JAVA_HOME set to a jre for Java 1.6. It's still not clear to me how Eclipse uses this, but it appears to be defaulting to this and not taking the project overrides. I have also installed Java 1.5, and added a JRE for this in eclipse in the Java->Installed JREs section.

In my project, I've set the compiler compliance level to 1.5. In the build path for the project, I've added the System Library for the Java 1.5 JRE. However, I'm getting compile errors for a class that implements PreparedStatement for not implementing abstract methods that only exist in Java 1.6 PreparedStatement. Specifically, the methods

setAsciiStream(int, InputStream, long) and 
setAsciiStream(int, InputStream)

Strangely enough, it worked when we were compiling it against Java 1.4, which it was originally written for. We added the JREs for Java 1.4 and referenced that system library in the project, and set the project's compiler level to 1.4, and it works fine. But when I do the same changes to try to point to Java 1.5, it instead uses 1.6.

Any ideas why?

+1  A: 

First off, are you setting your desired JRE or your desired JDK?

Even if your Eclipse is set up properly, there might be a wacky project-specific setting somewhere. You can open up a context menu on a given Java project in the Project Explorer and select Properties > Java Compiler to check on that.

If none of that helps, leave a comment and I'll take another look.

Lord Torgamus
I'm trying to set the desired JDK. All the relevant info should be in the original post and the comment on the other answer.
It was quite intentional that I asked what you were setting, not what you were trying to set. You said JRE several times in the question but didn't say JDK once. In any case, I'll check out your other comment.
Lord Torgamus
+1  A: 

From the menu bar: Project -> Properties -> Java Compiler

Enable project specific settings (checked) Uncheck "use Compliance from execution environment '.... Select the desired "compiler compliance level"

That will allow you to compile "1.5" code using a "1.6" JDK.

If you want to acutally use a 1.5 JDK to produce "1.5" compliant code, then install a suitable 1.5 JDK and tell eclipse where it is installed via:

Window -> preferences -> Installed JREs

And then go back to your project

Project -> properties -> Java Build Path -> libraries

remove the 1.6 system libaries, and: add library... -> JRE System LIbrary -> Alternate JRE -> The JRE you want.

Verify that the correct JRE is on the project's build path, save everything, and enjoy!

Edwin Buck
I had already done all that, except for unchecking 'Use default compliance settings' for the project Java compiler. I unchecked it, cleaned and rebuilt my project, and still had the Java 1.6 errors. I'm using Eclipse 3.3 in case that matters. I noticed that the 'Use default compliance settings' setting for the project didn't save, even after I hit 'Apply' and OK. I even deleted the java 1.6 location that JAVA_HOME was pointing to. So now I really don't know where its getting the Java 1.6 compiler from.
It's not clear if you're setting JAVA_HOME before you run Eclipse, or if you are setting JAVA_HOME in some sort of dialog box within the Eclipse application.If before you run Eclipse, you're changing the JVM that Eclipse is running inside of; best to undo your changes, as you are solving the wrong problem.If you're setting it within an Eclipse dialog, the pseudo slave environment Eclipse launches doesn't override the Eclipse configured JVM you specify to launch. Can you see the desired JDK in Windows -> Preferences -> Installed JREs? What about in "Execution Environments"?
Edwin Buck
When I mention JAVA_HOME, I'm referring to the system variable setting (from My Computer properties) that exists before starting Eclipse. I'm not changing JAVA_HOME from inside Eclipse anywhere. Yes I can see the JDK 5 that I want in both Windows->Preferences->Installed JREs and Execution Environments. I'm not sure where all the JREs in Execution Environments are coming from. There is a JavaSE-1.6 there that indicates 'perfect match' for the Java 5 JRE.
That system JAVA_HOME has nothing to do with internal Eclipse environments.The JavaSE-1.6 supports compiling to Java 1.5. That's why it's a perfect match for the Java 5 targets. You don't need to install Java 1.5 to compile code which will work on 1.5 JVMs.If you want to compile with a Java 5 JDK only, alter the project build path by removing the JDK you're using now and including the system libraries for the 1.5 JDK you want to use. If you can't include the 1.5 JDK libraries, then odds are you didn't configure a JDK, but rather a JRE in the "Installed JREs and Execution Environments"
Edwin Buck
A: 

Just to clarify, do you have JAVA_HOME set as a system variable or set in Eclipse classpath variables? I'm pretty sure (but not totally sure!) that the system variable is used by the command line compiler (and Ant), but that Eclipse modifies this accroding to the JDK used

waitinforatrain
JAVA_HOME is set as a system variable. I don't think it's set in Eclipse classpath variables.
What I meant to say is that Eclipse sort of sets it depending on the JDK you have selected, didn't mean to specifically enter it in classpath vars.The point of what I was saying is that Eclipse doesn't use the system JAVA_HOME variable
waitinforatrain
Are you saying Eclipse sets its own JAVA_HOME based on the JDK you have selected? Where do you select the JDK?