88
votes

I recently downloaded JDK 1.8.0.0_06 that comes bundled with NetBeans. I then got to find out that my JavaFx project, that I have been developing in Java 1.7 won't compile in Java 1.8, and so I stepped down my JDK to 1.7. Still I can't get it to work/ compile. I get this error:

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

I'm really stuck and would greatly appreciate any help.

15
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 herejmj
You are a life saver @Jigar Joshi!!!! Thanks again***** a mill. Am so grateful. You could put your response as an answer so I can accept it. Wow! Again thank you. Had been stuck for so long!Program-Me-Rev
If your project won't compile under Java 1.8, you are likely doing something pretty badly wrong (for example, you have static fields for the UI components in the code you posted in your other quetion, which is a serious design error). It's probably better to fix that problem than just to revert to a version from which many users will upgrade at some point in the future.James_D
You are right @James_D. This is my first project in java. I pretty much taught myself programming, and wasn't getting the whole thing about avoiding static members, interfaces, and the rest about polymorphism till now. My project is a bit too big to be edited all at once. I plan to edit it and move to dependency injection via Spring bit by bit as I finish up on the other remaining areas. It's a painful lesson, but well learnt.Program-Me-Rev
There are Java versions 1.8.0, 1.8.0_05, and 1.8.0_11, but there is no such thing as 1.8.0.0_06.David Conrad

15 Answers

33
votes

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

67
votes

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

27
votes
  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.

23
votes

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

14
votes

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.

9
votes

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.

4
votes

Maven setting:

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

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.

2
votes

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.

1
votes

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

1
votes

Alternatively, I checked the pom.xml and changed

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

to

<java.version>1.7</java.version>
1
votes

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

1
votes

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

0
votes

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

-1
votes

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.