How to Set Java Environment Variables

How to Set Java Environment Variables

To start Java programming it is required to set up your Java environment properly to compile and run the programs on your system.

Environment variables are global system variables that are useful to store system-wide values and accessible by all the processes/users running on Operating System (OS), such as Windows, macOS, and Linux.

Java software environment (Java SE) is free to download. To install JDK on your local environment you can download it as per your local environment Operating System (Windows, Linux, Solaris, etc) and architecture (X32, X64, X86, etc).

To download JDK you can use this URL: https://www.oracle.com/java/technologies/javase-downloads.html

If your computer Operating System is Windows X64 and you want to use Java latest version like (Java SE Development Kit 14) you can download JDK-14.0.1_windows-x64_bin.exe on your computer. You can download the java version as per your requirement.

Once the Java installer is downloaded you can install it on your machine. It is very easy to install, only you need to run the .exe file and follow the installer GUI instructions. Once Java installation is completed successfully, set Java environment variables on your machine to point the Java installation directory. 

We set PATH and CLASSPATH environment variables to compile and run the Java Programs in Windows, Linux, and other environments and to find class files which are compiled in bytecode by the Java compiler.

PATH

The Path is an environment variable that is required to find JDK executables or commands to compile and run the Java programs in Windows, Linux, and other Operating Systems.

The Path is an environment variable having a series of directories separated by semicolons (;). Microsoft Windows looks for program directories in order from left to right in the Path variable. Only one JDK/bin directory should be given in the Path variable at a time.

Set Environment Variables on your machine

If you go to the Java installation JDK/ bin directory you would be able to see that all Java executables files (commands) like javac.exe, java.exe, etc lie under this folder. These executables files are required for Java Programs to compile, run, generate java documentation, etc. To run these executables (commands) conveniently we set the location of the JDK/bin directory folder in the path variable.

By specifying the java installation directory location for JDK/bin in the path variable, Java programs can be compiled and run easily. Otherwise every time we need to specify these executable files location to run the Program. Like if we want to compile Java Program1 and Program2 and so on, every time we need to specify the full directory path of javac command.

C:\Java\jdk1.7.0\bin\javac Program1.java

C:\Java\jdk1.7.0\bin\javac Program2.java

Set Environment Variables on your machine 

If Java path has been specified in the path environment variable we can directly use the command name. Like if we want to compile Java Program1 and Program2 and so on, every time we do not need to specify the full directory path of javac command we can directly use javac command.

javac Program1.java

javac Program2.java

Set-Environment-Variables

There are two ways to set the path in Java:

  1. Set Permanent Java path 
  2. Set Temporary Java path

Steps to set Permanent Java Path in Environment variables in Windows

  1. Go to windows ‘Computer’ -> Right Click on it -> select Properties ->
    Or
    Go to windows Start-> Control Panel->System and Security->System->
  1. In Left Side of the System screen click on “Advanced System Setting”

  2. Click on “Advanced” tab

  3. Click on the “Environment Variables” button

  4. Under the “System Variables” section search ‘Path’ variable, Select it and click on the “Edit” button. If you did not find the “Path” variable click on the “New” button and create a new “Path” variable.

    Edit or Add Java installation binary files (JDK/bin) location like ‘C:\Program Files\Java\jdk1.8.0_131\bin’   in the Path variable.

    (Assuming Java is installed in directory C:\Program Files\Java)

    The Path environment variable can have more than one values and all values will be semicolon (;) separated.

  5. Close all windows by clicking on the ‘Ok’ button
Permanent Java Path in Environment variables of windows

Another way to set Permanent Java Path in Environment variables in Window by creating a JAVA_HOME variable

  1. Go to desktop ‘Computer’ Icon -> Right Click on it -> select Properties ->
    Or
    Go to windows Start -> Control Panel -> System and Security->System->

  2. In Left Side of the System screen click on “Advanced System Setting”

  3. Click on “Advanced” tab

  4. Click on the “Environment Variables” button

  5. Search JAVA_HOME variable under ‘System variables’ or ‘User variable’. If it exists assign a new Java installation JDK folder location otherwise add a new environment variable named JAVA_HOME and give Java installation JDK folder location. JAVA_HOME=C:\Program Files\Java\jdk1.8.0_131

    (Assuming Java is installed in directory C:\Program Files\Java\)

  6. Search Path variable under ‘System Variables’ section, If It exists
    1. Check Path values for %JAVA_HOME%\bin. If exists just click on ‘Ok’ close all windows by clicking on Ok button
    2. Otherwise if %JAVA_HOME%\bin does not exist add it in Path value using Semicolon (;) separator and click on ‘Ok’ button. Close all windows by clicking on the ’Ok’ button.

  7. If the Path variable under ‘System Variables’ section does not exist
    1. Click on New button
    2. Give variable name ‘Path’
    3. Add value %JAVA_HOME%\bin;
    4. Click Ok and close all windows by clicking on Ok button
Another way to set Permanent Java Path in Environment variables in Window by creating a JAVA_HOME variable

Steps to set Temporary Java Path in Windows

To set the temporary Java Path follow below steps

  • Open the command prompt
  • Copy the location of the Java installation directory up to the JDK/bin folder
  • Write set command in command prompt

(Assuming Java is installed in directory C:\Program Files\Java)

set path=C:\Program Files\Javajdk1.8.0_131\bin

Steps to set Temporary Java Path in Windows

Setting Java Path in Linux

To set Java Path in the Linux export tool is used in place of the set command. To set Java Path in Linux, the command is export PATH=<JDK installation location>

(Assuming Java is installed in the home directory under Root (/home))

CLASSPATH

The CLASSPATH environment variable is used to find class files that were compiled in bytecode by the Java compiler.

To execute or run the Java Programs, JVM requires compiled files (.class files) location. CLASSPATH is a location or directory of Java compiled files (.class files) by the compiler which is used by Java Virtual Machine(JVM) to load and execute the java programs. To execute or run compiled java programs from command prompt it is required to specify compiled java classes location to ‘java’ command. If CLASSPATH is not set you can get an error message. like “Could not find or load main class xyz.java .” where xyz is a filename.

CLASSPATH can be set in 2 ways

  • Set Permanent Java CLASSPATH
  • Set Temporary CLASSPATH

Set Permanent Java CLASSPATH  in Environment variables

If you want to run multiple java programs using command prompt it is a good option to set CLASSPATH in the environment variable. Once  CLASSPATH  is set, compiled Java classes(bytecode) will be picked to run automatically.

  1. Go to desktop ‘Computer’ Icon->Right Click on it->select Properties->
    Or
    Go to windows Start-> Control Panel->System and Security->System->

  2. In Left Side of the System screen click on “Advanced System Setting”

  3. Click on “Advanced” tab

  4. Click on the “Environment Variables” button

  5. Edit or Add a new Java CLASSPATH variable under ‘User Variables’. Give the Java compiled files (.class files) location as a value to this variable. The CLASSPATH environment variable is case insensitive.

    (Assuming compiled files (.class files) location is E:\shared\javaclasses)

  6. Close all windows by clicking on the ‘Ok’ button.
Set Permanent Java CLASSPATH  in Environment variables

Set Temporary CLASSPATH in windows

 The temporary CLASSPATH can be set in 2 ways to run java programs

  1. Temporary classpath can be set by using set CLASSPATH command in windows
  2. By using -cp or -classpath command-line option

   1. Temporary CLASSPATH by using set CLASSPATH command

To run or execute Java programs from the command prompt you can use the “set CLASSPATH” command. This classpath will be available until the command prompt will not be closed. It will set the directory location of compiled Java files (.class files) and you can run any program easily by using ‘java’ command in the command prompt.

If you want to use a new command prompt you would be required to set CLASSPATH again.

For this option to use a new command prompt each time you need to run the “set CLASSPATH “ command to run the program.

  (Assuming compiled files (.class files) location is E:\shared\javaclasses)

set CLASSPATH= E:\shared\javaclasses

Temporary CLASSPATH by using set CLASSPATH command

2. Using -cp or -classpath command-line option

We can use -cp or –classpath command-line option to run java programs. We give compiled files location with -cp/-classpath command-line. This command-line option is required each time with ‘java’ command whenever you want to run the program. To run Java program  ‘java’ command will run with .class file name followed by -cp or -classname command-line option.

Using -cp or -classpath command-line option -1

 (Assuming compiled files (.class files) location is E:\shared\javaclasses and .class file name is Program1 under package com.java)

Java –cp E:\shared\javaclasses com.java.Program1

Using -cp or -classpath command-line option -2

Difference Between PATH and CLASSPATH

The PATH environment variable is different from CLASSPATH as PATH is used to be known for Java commands or executables file location while CLASSPATH is basically to be known for java compiled files (Java Byte code) location.

If the Java installation PATH is not defined, you cannot run any of the java commands from the command prompt.

If CLASSPATH is not defined you cannot run/execute compiled Java programs. If PATH is defined but CLASSPATH is not defined, you can execute any of the Java commands but it could be possible your program will not execute as JVM will have trouble locating compiled files location.

You can get the error something like “Could not find or load main class xyz” (where xyz is .class name)  while trying to run the java program.

If PATH is not defined but CLASSPATH is defined then It will make no sense as it will not be able to find Java commands. You can get errors such as “<command name> is not recognized internal or external command…..” while trying to compile any java program.

2 thoughts on “How to Set Java Environment Variables

  1. Wonderful blog! I found it while browsing on Yahoo
    News. Do you have any suggestions on how to get
    listed in Yahoo News? I’ve been trying for a while but I never seem to get there!
    Many thanks

Leave a Reply

Your email address will not be published. Required fields are marked *