If you don't have admin rights, use the below command to set environment variables for java using the command prompt
Copysetx JAVA_HOME "C:\Program Files\Java\jdk1.8.0"
setx PATH "%PATH%;%JAVA_HOME%\bin";
Modify the environment variable.
Copysetx -m JAVA_HOME "C:\Program Files\Java\jdk1.8.0"
setx -m PATH "%PATH%;%JAVA_HOME%\bin";
Answer from Sathiamoorthy on Stack OverflowIf you don't have admin rights, use the below command to set environment variables for java using the command prompt
Copysetx JAVA_HOME "C:\Program Files\Java\jdk1.8.0"
setx PATH "%PATH%;%JAVA_HOME%\bin";
Modify the environment variable.
Copysetx -m JAVA_HOME "C:\Program Files\Java\jdk1.8.0"
setx -m PATH "%PATH%;%JAVA_HOME%\bin";
Set the JAVA_HOME Variable
Windows 7 – Right click My Computer and select Properties > Advanced
Windows 8 – Go to Control Panel > System > Advanced System Settings
Windows 10 – Search for Environment Variables then select Edit the system environment variables

Click the Environment Variables button.
Under System Variables, click New.
In the Variable Name field, enter either:
JAVA_HOME if you installed the JDK (Java Development Kit)
or
JRE_HOME if you installed the JRE (Java Runtime Environment). In the Variable Value field, enter your JDK or JRE installation path .

Open Command Prompt as Administrator.
Set the value of the Environment variable to your JDK (or JRE) installation path as follows:
setx -m JAVA_HOME "C:\path_to_Java\jdk_version"
Videos
You can set your JAVA_HOME in /etc/profile as Petronilla Escarabajo suggests. But the preferred location for JAVA_HOME or any system variable is /etc/environment.
Open /etc/environment in any text editor like nano or gedit and add the following line:
JAVA_HOME="/usr/lib/jvm/open-jdk"
(java path could be different)
Use source to load the variables, by running this command:
source /etc/environment
Then check the variable, by running this command:
echo $JAVA_HOME
Update
Usually most linux systems source /etc/environment by default. If your system doesn't do that add the following line to ~/.bashrc (Thanks @pje)
source /etc/environment
To set JAVA_HOME environment variable, do the following:
- Launch Terminal by pressing Ctrl+Alt+T on your keyboard.
- Enter the following command:
$gksudo gedit /etc/environment - Depending on where you installed your Java, you will need to provide the full path. For this example, I installed Oracle JDK 7 in the
/usr/lib/jvm/java-7-oracledirectory.
Scroll to the end of the file and enter the following:
JAVA_HOME=/usr/lib/jvm/java-7-oracle
export JAVA_HOME - Save your file and exit gedit.
- Lastly, reload the system PATH with the following command:
$. /etc/environment
The above method will save you the hassle in having to run the commands every time you log in to your computer.
Hello! I'm very (very) new to programming. I'm following a tutorial by Kaupenjoe to make (what I thought would be) a very simple Minecraft mod. But when I get to the step in the tutorial at 12:30 and run the terminal command ./gradlew genIntellijRuns, I receive the following error message. I'm still trying to figure out how all of this works, so pardon my ignorance if this is a stupid question, but I cannot for the life of me find how to correct this error. Could anyone walk me through it, please?
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.