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
- Open the Control Panel. The steps to do this vary depending on your version of Windows:
- 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.
- Windows 7 and earlier: Click the Start menu and select Control Panel.
- Click System. If you don't see this option, click System and Security first, then click System.
- Click Advanced system settings in the left column. Depending on your settings, you may be prompted to enter your administrator password to continue.
- Click the Environment Variables button. It's near the bottom of the window.
- 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".
- If there is no Path variable under "System Variables," click New below that section, then type Path into the "Variable Name" field.
- Add the path variable.
- If you clicked Edit in the last step, click New near the top-right corner of the window, and then enter the desired path.
- If you had to create a path variable, in the last step by clicking New, type the desired path into the "Variable Value" field.
- 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
- 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.
- 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]
- 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:
- Note the version number you want to use as your default installation (e.g., 1.8.0_121).
- Type export JAVA_HOME=`/usr/libexec/java_home -v version_number`, replacing version_number with the actual version (e.g., 1.8.0_121).
- Press ⏎ Return.
- 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:
- Type $ cd ~/ and press ⏎ Return. This ensures you're in your user directory.
- Type ls -a and press ⏎ Return to see the contents of your user directory.
- 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.
- 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.
- 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).
- Save the file and close the text editor.
- 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
- 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.
- Type java -version and press ↵ Enter. If you already have a Java version in your path, this will display the version number.
- If you see the correct version of Java, your path is already set.
- If you see "java: command not found," the path is not set, and you should continue with this method.
- 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.
- 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.
- Add the path variable to your source file. The syntax is different depending on your shell:[3]
- First, if you're using Vi, press i to get into insert mode.
- 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.
- 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.
- Save and exit the file. If you're using Vi, press Esc to go into command mode, type :wq! and press ↵ Enter.
- 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:
- bash: . /.profile.
- csh: source ~/.cshrc.
- 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
You should read it
- How to Find a File's Path on Windows
- Press type in C
- How to Install JDK in Linux
- What is Windows PATH?
- How to fix the long path of the directory path on Windows
- Did you know Windows 10 solved the path problem longer than 260 characters? Read the article below
- How to add Python to the Windows PATH variable
- How to set the JAVA_HOME path in Debian 10
- Data type in C #
- 106 tips with Microsoft Office - Part 3
- File path in HTML
- How to Stop an Application from Opening at Startup on a Windows XP PC
Maybe you are interested
6 ways to copy file and folder paths in Windows 11
4 walking paths across the sea in Vietnam
How to fix 'Windows Cannot Access the Specified Device, Path or File' error
11 ways to fix 'The System Cannot Find The Path Specified' error on Windows
Warpath strategy tips for beginners
Copying file and folder paths in File Explorer is now much simpler on Windows 11