Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I get eclipse to use a different compiler version for Java?

Tags:

java

eclipse

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?

like image 931
user26270 Avatar asked Mar 29 '10 19:03

user26270


People also ask

How do I fix Java version mismatch in Eclipse?

Before exporting the jar file from eclipse select the version 1.6 or 1.7 rather than 1.8. Please follow the below steps: (i) Right Click on project and go to "Properties". (ii) Now Select "Java Compiler" and uncheck use compliance from execution.

How do I switch to Java 11 in Eclipse?

A Java 11 JRE is recognized by Eclipse for launching. It can be added from the Window > Preferences > Java > Installed JREs > Add... page. It can also be added from the Package Explorer using the project's context menu.


1 Answers

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!

like image 151
Edwin Buck Avatar answered Oct 07 '22 20:10

Edwin Buck