如何为 Eclipse 项目更改 JDK 版本

我需要写一个只与 Java 1.5兼容的项目。我安装了 Java 1.6。是否存在某种形式的向后兼容性,以便用1.5编译 Eclipse?

我必须安装 Java 1.5才能在这里显示它吗? 或者可能还有其他方法?

Enter image description here.

416217 次浏览

Click on the Add Library button. It brings your screen to point to the Java location.

Select "Directory", button right besides JRE home and point to the installed folder location.

Even though you want to just 1.5 compiler project, you can achieve it by changing compiler settings in Eclipse instead of removing 1.6 JRE and add 1.5 JRE.

GOTO -->JAVA--Compiler---> and change compiler level to `1.5` instead of `1.6`

As davidfmatheson suggested,

Just be careful, especially if you're setting this up for a team of people to work on. If anyone uses anything that is new or changed in 1.6, it will compile, but not run in an environment with JRE 1.5.

See the page Set Up JDK in Eclipse. From the add button you can add a different version of the JDK...

In the preferences section under Java -> Installed JREs click the Add button and navigate to the 1.5 JDK home folder. Then check that one in the list and it will become the default for all projects:

enter image description here

Click on the Window tab in Eclipse, go to Preferences and when that window comes up, go to JavaInstalled JREsExecution Environment and choose JavaSE-1.5. You then have to go to Compiler and set the Compiler compliance level.

Changing JRE

Enter image description here

The JDK (JAVA_HOME) used to launch Eclipse is not necessarily the one used to compiled your project.

To see what JRE you can select for your project, check the preferences:

GeneralJava Installed JRE

By default, if you have not added any JRE, the only one declared will be the one used to launched Eclipse (which can be defined in your eclipse.ini).

You can add any other JRE you want, including one compatible with your project.

After that, you will need to check in your project properties (or in the general preferences) what JRE is used, with what compliance level:

Alt text
(source: standartux.fr)

Eclipse - specific Project change JDK Version -

If you want to change any jdk version of A specific project than you have to click ---> Project --> JRE System Library --> Properties ---> Inside Classpath Container (JRE System Library) change the Execution Environment to which ever version you want e.g. 1.7 or 1.8.

Right click project -> Properties -> Java Build Path -> select JRE System Library click Edit and select JDK or JRE after then click Java Compiler and select Compiler compliance level to 1.8

enter image description here

enter image description here

enter image description here

As I was facing this issue minutes ago, in case you are trying to open an existing project in an environment with a newer JDK, make sure you pdate the JDK version in Project Properties -> Project Facets -> Java.

If you are using maven build tool then add the below properties to it and doing a maven update will solve the problem

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>