8

I've seen the numerous other posts on here regarding JAVA_HOME variables and I have tried them all.

When I try and execute any gradle related tasks from the terminal I receive the following error:

ERROR: JAVA_HOME is set to an invalid directory: C:\Program Files\Java\jdk1.8.0_191

Please set the JAVA_HOME variable in your environment to match the location of your Java Installation.

If I run WHERE java I get C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe and c:\jdk\bin\java.exe, which leads me to believe there's an issue somewhere with this.

I added C:\Program Files\Java\jdk1.8.0_191 to my path and added JAVA_HOME as a system variable pointing at C:\Program Files\Java\jdk1.8.0_191. What am I missing? I have been banging my head against this for a while now; any help would be greatly appreciated.

EDIT: I have confirmed that C:\Program Files\Java\jdk1.8.0_191 does in fact exist and the contents are

  • bin
  • include
  • jre
  • lib
  • man
  • COPYRIGHT
  • javafx-src.zip
  • LICENSE
  • README
  • release
  • src.zip
  • THIRDPARTYLICENSEREADME
  • THIRDPARTYLICENSEREADME-JAVAFX
StormsEdge
  • 854
  • 2
  • 10
  • 35
  • 4
    There is a quote in your path. – AndiCover Feb 04 '20 at 21:00
  • What is producing the error message? ..check the contents of the (java) folders... – xerx593 Feb 04 '20 at 21:03
  • Set `C:\jdk` into `JAVA_HOME` variable. – Arvind Kumar Avinash Feb 04 '20 at 21:05
  • @AndiCover It was a typo when I copy pasted. There is not actually a quote in my path variable. Apologies – StormsEdge Feb 04 '20 at 21:06
  • @Arvind That depends how Java is installed. – OneCricketeer Feb 04 '20 at 21:06
  • Also, if you're going to down vote a post; please give a reason why. I have provided all information, described attempts to fix, and have edited my typo. – StormsEdge Feb 04 '20 at 21:07
  • @Storms Do you need Java 8 still?? https://adoptopenjdk.net/installation.html#x64_win-jdk – OneCricketeer Feb 04 '20 at 21:07
  • @cricket_007 Yes, the project i'm working with is Java 8. I'm using my company machine, which is why i'm dancing with config issues again. – StormsEdge Feb 04 '20 at 21:07
  • Are you using IntelliJ or Eclipse with your Gradle? Those have their own embedded Gradle configs and JREs. Do you need to use the terminal with Gradle? – OneCricketeer Feb 04 '20 at 21:08
  • Also, Java 11 can still compile Java 8 projects. I'm doing it right now – OneCricketeer Feb 04 '20 at 21:09
  • @cricket_007 I'm running Intellij and was directed to use 191 by the project manager. – StormsEdge Feb 04 '20 at 21:15
  • Can you upload a screenshot of this? https://www.jetbrains.com/help/idea/gradle.html#gradle_settings_access – OneCricketeer Feb 04 '20 at 21:16
  • Do you get same errors when doing this? https://www.jetbrains.com/help/idea/work-with-gradle-tasks.html# – OneCricketeer Feb 04 '20 at 21:17
  • 1
    Sidenote: This is really a case where having Vagrant or Docker build environments comes in handy. – OneCricketeer Feb 04 '20 at 21:22
  • Pardon me if this is a silly question, but I can't see anywhere in this entire post where it says that folder `C:\Program Files\Java\jdk1.8.0_191` actually exists on your computer. So does it? And if it does, what files and folders does it contain? – Abra Feb 04 '20 at 21:27
  • @Abra Edited question to reflect. Excellent point – StormsEdge Feb 04 '20 at 21:33
  • Open a command prompt window and type this command: `javac -version`. – Abra Feb 04 '20 at 21:42
  • @Abra returns: javac 1.8.0_191 – StormsEdge Feb 04 '20 at 21:43
  • 1
    Have you seen: [Gradle finds wrong JAVA_HOME even though it's correctly set](https://stackoverflow.com/questions/22307516/gradle-finds-wrong-java-home-even-though-its-correctly-set/44168551) And what about: [Windows 10 Gradle: JAVA_HOME is set to an invalid directory](https://stackoverflow.com/questions/44081972/windows-10-gradle-java-home-is-set-to-an-invalid-directory) And then there is: [JAVA_HOME is set to an invalid directory but it is set to the right directory](https://stackoverflow.com/questions/46414769/java-home-is-set-to-an-invalid-directory-but-it-is-set-to-the-right-directory) – Abra Feb 04 '20 at 21:44
  • You could always try using the short name, i.e. `PROGRA~1` instead of `Program Files`. The command `dir /X` will display the short name. – Abra Feb 04 '20 at 21:49
  • have you managed to fix the issue? – Lionel Cichero Feb 11 '20 at 03:30
  • @LionelCichero No unfortunately not – StormsEdge Feb 11 '20 at 04:48
  • have you tried https://stackoverflow.com/a/60065747/6656759? I remember once having almost the same issue and managed to fix it that way. – Lionel Cichero Feb 11 '20 at 21:25

8 Answers8

7

Change your path variable to include %JAVA_HOME%\bin

Remove quote from your JAVA_HOME, it should just be C:\Program Files\Java\jdk1.8.0_191

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
rhowell
  • 1,165
  • 8
  • 21
4

On linux you would do:

export JAVA_HOME="/usr/lib/jvm/java-7-openjdk-amd64/jre"
not
export JAVA_HOME="/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java"

June7
  • 19,874
  • 8
  • 24
  • 34
Elmasry
  • 112
  • 1
  • 10
3

If you check your PATH variable you will probably find that this directory "C:\Program Files (x86)\Common Files\Oracle\Java\javapath" appears before your JAVA_HOME directory.

Try changing the order, for example:

Instead of:

C:\Program Files (x86)\Common Files\Oracle\Java\javapath;%JAVA_HOME%\bin

Make it like this:

%JAVA_HOME%\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath
Lionel Cichero
  • 553
  • 4
  • 17
1

set JAVA_HOME="C:\Program Files\Java\jdk1.8.0_101" (note that: you should set it to your own path)

set Path="%JAVA_HOME%\bin"

(very important)

finish.

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
wang lulu
  • 11
  • 1
0

Well, you will define JAVA_HOME, if your installation was default probably the path is "C:\Program Files (x86)\Java\jdk1.8.0_60" and then you will define Path variable with %JAVA_HOME%\bin;

Some tutorials tell you to config the Classpath as well, but this is not necessary.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

I was creating a maven project and while creating the project I was getting the same error. I have installed Java in H drive. the installed files are in H:\Java. In environment variable I did two things. first I created a local variable Variable name = JAVA_HOME. Variable value = H:\Java

Second in path variable I added one more path. %JAVA_HOME%; H:\JAVA; and restarted the IDE again and it worked

0

For me the issue was with the way openjdk sets JAVA_HOME variable.

  1. Make sure your JAVA_HOME variable is set like this

    export JAVA_HOME="/usr/local/opt/openjdk@17"

// NOTICE THERE IS NO /bin appended

  1. Then for the PATH variable you can append the "/bin"

    export PATH="$PATH:$JAVA_HOME/bin"

Maxim M
  • 306
  • 2
  • 13
-1

you can go on environment variables and browse the path of the java jdk you want to use. the higher jdk does not seem to work properly with java environments.

here, you are using jdk 11 instead of that you can use lower version jdk like: jdk 1.8_0_201.