Setting JAVA_HOME for Gradle

While trying to set up Gradle, a software develpment build automation tool, on a Microsoft Windows 10 system, when I ran the gradle.bat file in the gradle bin directory, I saw the message below:

C:\Users\Jim\Downloads\gradle-7.5.1-bin\gradle-7.5.1\bin>gradle.bat

ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.

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

C:\Users\Jim\Downloads\gradle-7.5.1-bin\gradle-7.5.1\bin>

I had installed Java with the Eclipse integrated development environment (IDE) previously and the java.exe executable was installed beneath the C:\Users\Jim\.p2\pool\plugins\org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_18.0.1.v20220515-1614\jre\ directory, so I created a JAVA_HOME environment variable pointing to that directory that applied to all accounts on the system. You can create a temporary JAVA_HOME environment variable for the account under which you are currently logged in from a command line interface (CLI) as noted at Running java from an Eclipse installation from the command line, but I wanted to create a permanent environment variable so I typed advanced system settings in the Windows "Type here to search" field and then clicked on View advanced system settings when I saw that listed.

View advanced system
settings

You will need to provide authorization credentials for an administrator account to modify the settings. When you do so, under the Advanced tab for System Properties, you will see an Environment Variables button.

System Properties - Advanced

When you click on the Environment Variables button, you will see the environment variables for the administrator account you are using at the top of the window and the system environment variables that apply to all accounts on the system at the bottom.

Environment Variables

If you click on New, you can create a new system varible.

New system variable

I set JAVA_HOME to be the jre directory beneath the Eclipse installaton, i.e., C:\Users\Jim\.p2\pool\plugins\org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_18.0.1.v20220515-1614\jre\.

New JAVA_HOME

I could then see a new system variable, in this case JAVA_HOME, in the System Variables section of the environment variables.

System Variables - JAVA_HOME

After you have created the new environment variable you want, you can click on OK to close the window. When I opened a new command prompt window and issued the command echo %java_home%, I could see the path for that variable. Note, you won't see a new environment variable in a command prompt window you already have open.

C:\Users\Jim>echo %java_home%
C:\Users\Jim\.p2\pool\plugins\org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_18.0.1.v20220515-1614\jre\

C:\Users\Jim>

Related articles:

  1. Running java from an Eclipse installation from the command line
  2. Obtaining a command prompt in Windows 10