无效的目标版本: 1.8

我最近下载了与 NetBeans 绑定的 JDK 1.8.0.0 _ 06。然后我发现我在 Java 1.7中开发的 JavaFx 项目不能在 Java 1.8中编译,所以我把 JDK 降到了1.7。我还是不能让它工作/编译。我得到了这个错误:

javac: invalid target release: 1.8
Usage: javac <options> <source files>

我真的卡住了,非常感谢你的帮助。

174067 次浏览

if you are going to step down, then change your project's source to 1.7 as well,

right click on your Project -> Properties -> Sources window

and set 1.7 here

note: however I would suggest you to figure out why it doesn't work on 1.8

Your javac is not pointing to correct java.

Check where your javac is pointing using following command -

update-alternatives --config javac

If it is not pointed to the javac you want to compile with, point it to "/JAVA8_HOME/bin/javac", or which ever java you want to compile with.

  1. Download JDK 8.
  2. Edit your env var for JAVA_HOME to point to the new installed JDK 1.8: R-click on My Computer -> Properties -> Advanced System Settings -> Environment Variables find JAVA_HOME on the list and set it's value to something like that:

    C:\Program Files\Java\jdk1.8.0_31
    
  3. Find the old version and edit it in PATH variable aswell.

  4. Probably you will need to restart the system.

Should be done.

For IntelliJ14 you may have to change the bytecode version w.r.t. the JDK you are using (in the global settings):

enter image description here

Maven setting:

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

If this error occurs when running a Gradle (or Maven) task, you need to modify that build tool configuration to point to your installation of Java JDK 1.8 following this route:

File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle

There you check the Linked Gradle project is the one you are working on and select the Gradle JVM (You missed this when you imported the gradle project into IntelliJ)

enter image description here

Remember when importing a Gradle (or Maven) project to set the target JVM correctly here: enter image description here

if you are going to step down, then change your project's source to 1.7 as well,

right click on your Project -> Properties -> Sources window and set 1.7 here" Jigar Joshi

Also go to the build-impl.xml and look for the property excludeFromCopy="${copylibs.excludes}" and delete this property on my code was at line 827 but I`ve seen it on other lines

for me was taking a code from MAC OS java 1.8 to WIN XP java 1.7

Alternatively, I checked the pom.xml and changed

<java.version>1.8</java.version>

to

<java.version>1.7</java.version>

Most of the time, these type of issues happen due to incorrect java version. Make sure your PATH and JAVA_HOME variables are pointing to the correct version.

None of the previous solutions worked for me.

I solved it by editing .idea/compiler.xml There were "extra" (1) and (2) copies of the bad module with different targets. I deleted the extraneous entried and changed the targets in the section to 1.8 and it worked.

I got the same issue with netbeans, but mvn build is OK in cmd window. For me the issue resolved after changing netbeans' JDK (in netbeans.conf as below),

netbeans_jdkhome="C:\Program Files\Java\jdk1.8.0_91"


Edit: Seems it's mentioned here: netbeans bug 236364

For IntelliJ IDEA Ultimate latest version as of 18th Dec 2017, if the above suggestions don't work, then please try the following: Right Click on the project and navigate to "Open Module Settings". Open it, then change the "Language Level" from the dropdown.

I got the same issue in IntelliJ IDEA Community with Maven and I had to reimport the project by right-clicking the project in the Project tab -> Maven -> Reimport

Just do this. Then invalidate IntelliJ caches (File -> Invalidate Caches)

Installing a newer release of IDEA Community (2018.3 instead of 2017.x) was solved my issue with same error but java version:11. Reimport hadn't worked for me. But it worth a try.

I've simply tricked the build system and put Java 8 path into Java 7 section of my Maven's toolchains.xml:

  <toolchain>
<type>jdk</type>
<provides>
<version>1.7.0_80</version>
<vendor>sun</vendor>
</provides>
<configuration>
<!--jdkHome>C:\Program Files\java\jdk1.7.0_80</jdkHome-->
<jdkHome>C:\Program Files\java\jdk1.8.0_281</jdkHome>
</configuration>
</toolchain>