ChatGPT解决这个技术问题 Extra ChatGPT

Setting JDK in Eclipse

I have two JDKs, for Java 6 and 7.

I want to build my project using both. Initially we only built against 1.6. I see in my project setting I can select 1.5, 1.6 1.7 as the compiler level.

How are these options added to the IDE? I never installed Java 1.5. Suppose I wanted Java 1.4. How would I get this to appear in the list? I have added two different JREs in my IDE preferences, but these are not what appears in the dropdown.


M
Manuel Jordan

You manage the list of available compilers in the Window -> Preferences -> Java -> Installed JRE's tab.

In the project build path configuration dialog, under the libraries tab, you can delete the entry for JRE System Library, click on Add Library and choose the installed JRE to compile with. Some compilers can be configured to compile at a back-level compiler version. I think that's why you're seeing the addition version options.


I had added my own JRE via the "Add" option in installed JREs window. Problem is when I checked mthe one I wanted Eclipse to use it did not get picked up as the Default workspace JRE. So when I looked in my project properties the "JRE System Library" was still there. When I deleted it and selected "Use default Workspace JRE" only then did it pick up my change. I would have thought Eclipse should have updated my projects JRE when I selected a different JRE for my workspace
Even though it says "use default" it's really using the default at the time the project was created/imported and it never looks back.
For those who can't find the 'Preferences' item, it's under the 'Window' dropdown menu.
I added a new JDK through windows preferences route, then deleted and reimported all my projects and Eclipse still chose the prior JDK. I had to update project build path configuration manually. Eclipse never fails to disappoint
G
Georgie

Some additional steps may be needed to set both the project and default workspace JRE correctly, as MayoMan mentioned. Here is the complete sequence in Eclipse Luna:

Right click your project > properties

Select “Java Build Path” on left, then “JRE System Library”, click Edit…

Select "Workspace Default JRE"

Click "Installed JREs"

If you see JRE you want in the list select it (selecting a JDK is OK too)

If not, click Search…, navigate to Computer > Windows C: > Program Files > Java, then click OK

Now you should see all installed JREs, select the one you want

Click OK/Finish a million times

Easy.... not.


I had to change my JAVA_HOME environment variable to the JDK's folder too, and the JRE was not enough.
Thank you Georgie! Websters defines default as, "a selection automatically used by a program in the absence of a choice made by the user". Yet Eclipse's "Workspace Default JRE" is not selected by default.
Thanks @Georgie, You saved my hours :)
G
Gunta Vaishnavi

To tell eclipse to use JDK, you have to follow the below steps.

Select the Window menu and then Select Preferences. You can see a dialog box. Then select Java ---> Installed JRE’s Then click Add and select Standard VM then click Next In the JRE home, navigate to the folder you’ve installed the JDK (For example, in my system my JDK was in C:\Program Files\Java\jdk1.8.0_181\ ) Now click on Finish.

After completing the above steps, you are done now and eclipse will start using the selected JDK for compilation.


J
Jose Manuel Gomez Alvarez

Configuring JDKs

Windows -> Preferences -> Installed JREs, to configured the installed JDKs Project Properties, Java Compiler, Enable project specific settings (or configure Workspace settings), JDK Compliance Project Properties, Java Build Path, Libraries, Add Library, JRE system library, Workspace default or Alternate JRE (one of the JREs configured in

Maven

BUT IF you are using maven, provided that you have your latest JRE (Windows/Preferences/Installed JREs) -for example JDK 1.8

You can select the level 1.6, 1.7, 1.8 by configuring the maven-compiler-plugin source and target attributes, like this

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

And ideally, if you have a parent pom, you can do it for all the modules (Eclipse projects) in the parent pom, in one single place.

Source and Target If we want to use the Java 8 language features the –source should be set to 1.8. Also, for the compiled classes to be compatible with JVM 1.8, the –target value should be 1.8.

Updating JRE library that is broken in many projects at once (with Maven)

Rather than updating one by one the JRE library, let Maven do it for you.

Selecting the projects and right-clicking for Maven -> Update Project, will set the system library to the path of the installed JDK, in case the paths are broken (because you installed a new JDK or imported from another computer, etc.) and set the JDK compliance according to the maven source and target setting in the pom.


G
Garrett Hall

Eclipse's compiler can assure that your java sources conform to a given JDK version even if you don't have that version installed. This feature is useful for ensuring backwards compatibility of your code.

Your code will still be compiled and run by the JDK you've selected.


J
JuanMoreno

JDK 1.8 have some more enrich feature which doesn't support to many eclipse .

If you didn't find java compliance level as 1.8 in java compiler ,then go ahead and install the below eclipse 32bit or 64 bit depending on your system supports.

Install jdk 1.8 and then set the JAVA_HOME and CLASSPATH in environment variable. Download eclipse-jee-neon-3-win32 and unzip : supports to java 1.8 Or download Oracle Enterprise Pack for Eclipse (12.2.1.5) and unzip :Supports java 1.8 with 64 bit OS Right click your project > properties Select “Java Compiler” on left and set java compliance level to 1.8 [select from the dropdown 1.8] Try running one java program supports to java 8 like lambda expression as below and if no compilation error ,means your eclipse supports to java 1.8, something like this: interface testI{ void show(); } /*class A implements testI{ public void show(){ System.out.println("Hello"); } }*/ public class LambdaDemo1 { public static void main(String[] args) { testI test ; /*test= new A(); test.show();*/ test = () ->System.out.println("Hello,how are you?"); //lambda test.show(); } }


Welcome to StackOverflow and thanks for your attempt to help. Please consider improving the readbility of your answer, by learning about formatting. Taking the tour might support you in doing that.