javaeclipse

Eclipse AutoComplete Not Matching JDK Version


I am currently using the latest version of Eclipse IDE and have my project setup to use JDK8, however when I attempt to use the autocomplete it provides suggestions from future compiler versions. This can get really annoying as every time that I need to rely on the autocomplete, I have to double check the Javadoc information to see if it is compatible. What settings do I need to change to force eclipse to only show <JDK8 suggestions?

enter image description here enter image description here


Solution

  • Eclipse is doing exactly what you asked it to. The problem lies in the word 'compiler compliance'; this has evidently confused you.

    It means language features. Specifically, it does not mean 'library support level'.

    What you want is for methods introduced in the java.* API that are not in the version you are targeting, to either be entirely non-existent (i.e. not in the autocomplete, and if you attempted to call them, red wavy underline), or possibly 'exists, but, marked off as unavailable / 'future'.

    This is not possible without giving eclipse an actual JDK of the right version, at which point, you get that behaviour if you configure the project to use that JDK. This more or less happens automatically if projects are configured right and in the preferences 'JRE' section you told eclipse where this JDK 'lives'.

    For example:

    1. Open eclipse preferences, filter to 'Installed JREs', add a JDK8 here.
    2. In the 'execution environments' subsection you can doublecheck that "JavaSE-1.8" is 'hooked up' to that JDK and not some newer one: Eclipse allows you to target JDK1.8 using e.g. a JDK24; it does this because if it did not, any project configured to target JDK1.8 would be utterly uneditable and unrunnable until you download, install, and tell eclipse about that JDK8 which is annoying. Hence, check that the checked JDK here is indeed JDK8.
    3. You can now go into the project's properties, section 'Build Path', tab 'Libraries' and doublecheck/update the modulepath/classpath section that refers to the JDK. It'll be referring to the JDK1.8 installation, or, if its not, you can set it to it here.

    Once you've done that, you have what you want.

    It doesn't matter what the compiler compliance level is set to. To highlight the point: With some acrobatics you can expose some JDK9+ java.* API to JDK8's javac and it would allow you to compile code that uses the newer APIs with it (and it would not run on a stock JDK8).

    Remember: "Java.* classpath" and "which lang features can I use" are essentially unrelated.

    For example, if you set compiler compliance to 10 or up, you can use var. If you set it to 9 or below, you cannot. If you set it to 9 or up you can make module-info.java files. 8 and below, you cannot do that.

    But none of that affects whether you can e.g. access JDK25's java.lang.IO.