How to Set the Path in Java

This wikiHow teaches you how to set the path for your Java installation on Windows, macOS, and Linux. If you're using a Windows PC or Mac and have just installed Java, there's typically no need to do this manually as the path is set during...
Method 1 of 3:

Windows

  1. How to Set the Path in Java Picture 1How to Set the Path in Java Picture 1
    Open the Control Panel. The steps to do this vary depending on your version of Windows:
    1. Windows 10 & 8: Press Windows Key + S to bring up the search bar, type control panel, and then click Control Panel in the search results.
    2. Windows 7 and earlier: Click the Start menu and select Control Panel.
  2. How to Set the Path in Java Picture 2How to Set the Path in Java Picture 2
    Click System. If you don't see this option, click System and Security first, then click System.
  3. How to Set the Path in Java Picture 3How to Set the Path in Java Picture 3
    Click Advanced system settings in the left column. Depending on your settings, you may be prompted to enter your administrator password to continue.
  4. How to Set the Path in Java Picture 4How to Set the Path in Java Picture 4
    Click the Environment Variables button. It's near the bottom of the window.
  5. How to Set the Path in Java Picture 5How to Set the Path in Java Picture 5
    Select the "Path" variable and click Edit. Choose the one that's under "System Variables" at the bottom of the window, not the one under "User Variables".
    1. If there is no Path variable under "System Variables," click New below that section, then type Path into the "Variable Name" field.
  6. How to Set the Path in Java Picture 6How to Set the Path in Java Picture 6
    Add the path variable.
    1. If you clicked Edit in the last step, click New near the top-right corner of the window, and then enter the desired path.
    2. If you had to create a path variable, in the last step by clicking New, type the desired path into the "Variable Value" field.
  7. How to Set the Path in Java Picture 7How to Set the Path in Java Picture 7
    Click OK and then OK again to save. Now that you've updated the Path, launch a new command prompt window (instead of the one you were using previously, if applicable) to run your java code.[1]
Method 2 of 3:

macOS

  1. How to Set the Path in Java Picture 8How to Set the Path in Java Picture 8
    Open a terminal window. If you haven't done so already, you can do so by typing terminal into the Spotlight search bar and clicking Terminal in the search results.
  2. How to Set the Path in Java Picture 9How to Set the Path in Java Picture 9
    Find the locations of your Java installations. You usually won't need to add or update the path unless you are switching between installed Java versions. To see which versions are installed (and their locations), type /usr/libexec/java_home –V and press Return.[2]
  3. How to Set the Path in Java Picture 10How to Set the Path in Java Picture 10
    Run the export command to set your path. If you only want to set the path for the current terminal window, use step. If you want to make the change permanent, skip to the next step. Here's how set the path for the current terminal only:
    1. Note the version number you want to use as your default installation (e.g., 1.8.0_121).
    2. Type export JAVA_HOME=`/usr/libexec/java_home -v version_number`, replacing version_number with the actual version (e.g., 1.8.0_121).
    3. Press Return.
  4. How to Set the Path in Java Picture 11How to Set the Path in Java Picture 11
    Add the export command to your shell profile. If you want to update the path permanently so you don't have to run export every time you open a terminal window, follow these steps:
    1. Type $ cd ~/ and press Return. This ensures you're in your user directory.
    2. Type ls -a and press Return to see the contents of your user directory.
    3. If you see a file called .bash_profile, great! If not, type touch .bash_profile and press Return to create it. If you use a different shell, ignore this step.
    4. Type open -e .bash_profile and press Return to open the file in your default text editor. If you use a shell other than bash, open your shell's profile in your desired text editor as you wish.
    5. Type export JAVA_HOME=`/usr/libexec/java_home -v version_number` on the first empty line in the file. Replace version_number with the version you want to set as the default (e.g., 1.8.0_121).
    6. Save the file and close the text editor.
    7. If you skipped step 3, type source .bash and press Return so the path is set for the current window.
Method 3 of 3:

Linux

  1. How to Set the Path in Java Picture 12How to Set the Path in Java Picture 12
    Open a terminal window. If you're not already at the command prompt, open a terminal window now. You can usually do this by pressing Ctrl+Alt+T.
  2. How to Set the Path in Java Picture 13How to Set the Path in Java Picture 13
    Type java -version and press Enter. If you already have a Java version in your path, this will display the version number.
    1. If you see the correct version of Java, your path is already set.
    2. If you see "java: command not found," the path is not set, and you should continue with this method.
    3. If you see a version number other than the one you want to use, continue with this method to add the desired Java version to your path.
  3. How to Set the Path in Java Picture 14How to Set the Path in Java Picture 14
    Open your shell startup file in a text editor. For example, if you use the bash shell, you could run vi .bashrc to open your shell startup file in the Vi text editor. If you use the C shell, you'd run vi .cshrc instead.
  4. How to Set the Path in Java Picture 15How to Set the Path in Java Picture 15
    Add the path variable to your source file. The syntax is different depending on your shell:[3]
    1. First, if you're using Vi, press i to get into insert mode.
    2. bash: Type PATH=/usr/local/jdk1.8.0/bin:$PATH onto one line, but replace /usr/local/jdk1.8.0 with the actual path to Java. On the next line, type export PATH.
    3. csh: Type set path=(/usr/local/jdk1.8.0/bin $path) on its own line, but replace /usr/local/jdk1.8.0 with the actual path to Java.
  5. How to Set the Path in Java Picture 16How to Set the Path in Java Picture 16
    Save and exit the file. If you're using Vi, press Esc to go into command mode, type :wq! and press Enter.
  6. How to Set the Path in Java Picture 17How to Set the Path in Java Picture 17
    Load the startup file. To make the changes take effect, you can either open a new terminal window or run the following command at the current prompt:
    1. bash: . /.profile.
    2. csh: source ~/.cshrc.
  7. How to Set the Path in Java Picture 18How to Set the Path in Java Picture 18
    Run java -version to check the path. Now that you've added the correct directory to your path, you should see the desired version when you run this command.
4 ★ | 1 Vote