Windows java path java home

How to set the environment variables for Java in Windows (the classpath)?

xpt's user avatar

xpt

20.7k37 gold badges128 silver badges217 bronze badges

asked Nov 4, 2009 at 7:54

Dean jones's user avatar

5

Java SE Development Kit 8u112 on a 64-bit Windows 7 or Windows 8

Set the following user environment variables (== environment variables of type user variables)

  • JAVA_HOME : C:\Program Files\Java\jdk1.8.0_112
  • JDK_HOME : %JAVA_HOME%
  • JRE_HOME : %JAVA_HOME%\jre
  • CLASSPATH : .;%JAVA_HOME%\lib;%JAVA_HOME%\jre\lib
  • PATH : your-unique-entries;%JAVA_HOME%\bin (make sure that the longish your-unique-entries does not contain any other references to another Java installation folder.

Note for Windows users on 64-bit systems:

Progra~1 = 'Program Files'
Progra~2 = 'Program Files(x86)'

Notice that these environment variables are derived from the «root» environment variable JAVA_HOME. This makes it easy to update your environment variables when updating the JDK. Just point JAVA_HOME to the fresh installation.

There is a blogpost explaining the rationale behind all these environment variables.

Optional recommendations

  • Add a user environment variable JAVA_TOOL_OPTIONS with value -Dfile.encoding="UTF-8". This ensures that Java (and tools such as Maven) will run with a Charset.defaultCharset() of UTF-8 (instead of the default Windows-1252). This has saved a lot of headaches when wirking with my own code and that of others, which unfortunately often assume the (sane) default encoding UTF-8.
  • When JDK is installed, it adds to the system environment variable Path an entry C:\ProgramData\Oracle\Java\javapath;. I anecdotally noticed that the links in that directory didn’t get updated during an JDK installation update. So it’s best to remove C:\ProgramData\Oracle\Java\javapath; from the Path system environment variable in order to have a consistent environment.

starball's user avatar

starball

22.1k8 gold badges47 silver badges286 bronze badges

answered Oct 29, 2014 at 21:00

Abdull's user avatar

AbdullAbdull

26.5k26 gold badges130 silver badges172 bronze badges

15

In Windows inorder to set

Step 1 : Right Click on MyComputer and click on properties .

Step 2 : Click on Advanced tab

alt text

Step 3: Click on Environment Variables

alt text

Step 4: Create a new class path for JAVA_HOME

alt text

Step 5: Enter the Variable name as JAVA_HOME and the value to your jdk bin path ie c:\Programfiles\Java\jdk-1.6\bin and

NOTE Make sure u start with .; in the Value so that it doesn’t corrupt the other environment variables which is already set.

alt text

Step 6 : Follow the Above step and edit the Path in System Variables add the following ;c:\Programfiles\Java\jdk-1.6\bin in the value column.

Step 7 :Your are done setting up your environment variables for your Java , In order to test it go to command prompt and type

 java   

who will get a list of help doc

In order make sure whether compiler is setup Type in cmd

  javac

who will get a list related to javac

Hope this Helps !

Taylor Hx's user avatar

Taylor Hx

2,81523 silver badges36 bronze badges

answered Nov 4, 2009 at 9:08

Srinivas M.V.'s user avatar

Srinivas M.V.Srinivas M.V.

6,5085 gold badges33 silver badges49 bronze badges

6

— To set java path —

There are two ways to set java path

A. Temporary

  1. Open cmd
  2. Write in cmd : javac

If java is not installed, then you will see message:

javac is not recognized as internal or external command, operable program or batch file.

  1. Write in cmd : set path=C:\Program Files\Java\jdk1.8.0_121\bin
  2. Write in cmd : javac

You can check that path is set if not error has been raised.

It is important to note that these changes are only temporary from programs launched from this cmd.

NOTE: You might have to run the command line as admin

B. Permanent

  1. Righ-click on «My computer» and click on properties
  2. Click on «Advanced system settings»
  3. Click on «Environment variables»
  4. Click on new tab of user variable
  5. Write path in variable name
  6. Copy the path of bin folder
  7. Paste the path of the bin folder in the variable value
  8. Click OK

The path is now set permanently.

TIP: The tool «Rapid Environment Editor» (freeware) is great for modifying the environment variables and useful in that case

TIP2: There is also a faster way to access the Environment Variables: press Win+R keys, paste the following %windir%\System32\rundll32.exe sysdm.cpl,EditEnvironmentVariables and press ENTER

Jean-Francois T.'s user avatar

answered Mar 20, 2017 at 7:04

Kimmi Dhingra's user avatar

0

In Windows 7, right-click on Computer -> Properties -> Advanced system settings; then in the Advanced tab, click Environment Variables… -> System variables -> New….

Give the new system variable the name JAVA_HOME and the value C:\Program Files\Java\jdk1.7.0_79 (depending on your JDK installation path it varies).

Then select the Path system variable and click Edit…. Keep the variable name as Path, and append C:\Program Files\Java\jdk1.7.0_79\bin; or %JAVA_HOME%\bin; (both mean the same) to the variable value.

Once you are done with above changes, try below steps. If you don’t see similar results, restart the computer and try again. If it still doesn’t work you may need to reinstall JDK.

Open a Windows command prompt (Windows key + R -> enter cmd -> OK), and check the following:

java -version

You will see something like this:

java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

Then check the following:

javac -version

You will see something like this:

javac 1.7.0_79

answered Nov 4, 2015 at 3:43

Arun's user avatar

ArunArun

2,3125 gold badges24 silver badges33 bronze badges

The JDK installation instructions explain exactly how to set the PATH, for different versions of Windows.

Normally you should not set the CLASSPATH environment variable. If you leave it unset, Java will look in the current directory to find classes. You can use the -cp or -classpath command line switch with java or javac.

answered Nov 4, 2009 at 8:26

Jesper's user avatar

JesperJesper

203k46 gold badges318 silver badges350 bronze badges

1

I am going to explain here by pictures for Windows 7.

Please follow the following steps:

Step 1:
Go to «Start» and get into the «My Computer» properties

enter image description here

Step 2: Go to «Advance System Setting» and click on it.

enter image description here

Step 3: Go to «Start» and get into the «My Computer» properties

enter image description here

Step 4: The dialog for Environment variable will open like this:

enter image description here

Step 5: Go to path and click on edit.

enter image description here

Step 6: Put the path of your JDK wherever it resides up to bin like you can see in the picture. Also add path from your sdk of Android up to the Platform Tools:

enter image description here

answered Jul 10, 2015 at 12:10

hitesh141's user avatar

hitesh141hitesh141

93312 silver badges25 bronze badges

In programming context you can execute SET command (SET classpath=c:\java) or Right click on your computer > properties > advanced > environment variables.

In a batch file you can use

SET classpath=c:\java
java c:\myapplication.class

answered Nov 4, 2009 at 8:03

Cem Kalyoncu's user avatar

Cem KalyoncuCem Kalyoncu

14.2k4 gold badges41 silver badges62 bronze badges

1

For Windows 7 users:

Right-click on My Computer, select Properties; Advanced; System Settings; Advanced; Environment Variables. Then find PATH in the second box and set the variable like in the picture below.

PATH variable editor

Victor Zamanian's user avatar

answered May 26, 2014 at 7:56

Zar E Ahmer's user avatar

Zar E AhmerZar E Ahmer

34k20 gold badges236 silver badges300 bronze badges

Java path set for java 11

  1. copy the path for jdk-11

Don’t include the \bin folder, just the JDK path. For example

CorrectC:\Program Files\Java\jdk-11

WrongC:\Program Files\Java\jdk-11\bin

In environmental variable, user variable section click on New button and give path like below.
enter image description here

after that give ok for it and go to the System variables and select the Path and double click on it.

enter image description here

click on new and paste %JAVA_HOME%\bin and click ok to all.
enter image description here

answered May 6, 2021 at 22:10

Supun Sandaruwan's user avatar

Set java Environment variable in Centos / Linux

/home/ vi .bashrc

export JAVA_HOME=/opt/oracle/product/java/jdk1.8.0_45

export PATH=$JAVA_HOME/bin:$PATH

java -version

answered Aug 9, 2016 at 4:42

Guna Sekaran's user avatar

0

Keep in mind that the %CLASSPATH% environment variable is ignored when you use java/javac in combination with one of the -cp, -classpath or -jar arguments. It is also ignored in an IDE like Netbeans/Eclipse/IntelliJ/etc. It is only been used when you use java/javac without any of the above mentioned arguments.

In case of JAR files, the classpath is to be defined as class-path entry in the manifest.mf file. It can be defined semicolon separated and relative to the JAR file’s root.

In case of an IDE, you have the so-called ‘build path’ which is basically the classpath which is used at both compiletime and runtime. To add external libraries you usually drop the JAR file in a (either precreated by IDE or custom created) lib folder of the project which is added to the project’s build path.

Vishal Yadav's user avatar

Vishal Yadav

3,6423 gold badges25 silver badges42 bronze badges

answered Nov 4, 2009 at 11:54

BalusC's user avatar

BalusCBalusC

1.1m373 gold badges3613 silver badges3557 bronze badges

2

  1. Download the JDK
  2. Install it
  3. Then Setup environment variables like this :
  4. Click on EDIT

enter image description here

  1. Then click PATH, Click Add , Then Add it like this:
    enter image description here

answered May 16, 2019 at 11:04

Abhishek Sengupta's user avatar

For deployment better to set up classpath exactly and keep environment clear.
Or at *.bat (the same for linux, but with correct variables symbols):

CLASSPATH="c:\lib;d:\temp\test.jar;<long classpath>"
CLASSPATH=%CLASSPATH%;"<another_logical_droup_of_classpath" 
java -cp %CLASSPATH% com.test.MainCLass

Or at command line or *.bat (for *.sh too) if classpath id not very long:

java -cp "c:\lib;d:\temp\test.jar;<short classpath>"

answered Nov 4, 2009 at 8:26

St.Shadow's user avatar

St.ShadowSt.Shadow

1,8501 gold badge12 silver badges16 bronze badges

For Windows:

  • Right click on ‘My Computers’ and open ‘Properties’.
  • In Windows Vista or Windows 7, go to «Advanced System Settings». Else go to next step.
  • Go to ‘Advanced Tab’ and click on Environment Variables button.
  • Select ‘Path’ under the list of ‘System Variables’, and press Edit and add C:\Program Files\java\jdk\bin after a semicolon.
  • Now click on ‘new’ button under system variables and enter ‘JAVA_HOME’ as variable name and path to jdk home directory (ex. ‘C:\Program Files\Java\jdk1.6.0_24’ if you are installing java version 6. Directory name may change with diff. java versions) as variable_value.

answered Sep 6, 2013 at 11:14

Pratap Singh's user avatar

Pratap SinghPratap Singh

4,6171 gold badge22 silver badges24 bronze badges

Your Keytools file sit under «Java/bin» folder so you need to either set Environment variable or go to «Java/bin» folder and run command

answered Aug 9, 2021 at 17:35

vaquar khan's user avatar

vaquar khanvaquar khan

10.9k5 gold badges73 silver badges96 bronze badges

You can add JAVA_HOME in the system environment variable from my computer>>advance tab>add the new path as explained here.

It might help Mac and Linux users as well.

answered Jun 12, 2022 at 5:14

Sunil's user avatar

SunilSunil

3,2193 gold badges21 silver badges21 bronze badges

If you plan to run software programs like Maven, Jenkins, Gradle or Tomcat, you’ll not only need a local installation of the JDK, but you’ll also want to ensure that you have set JAVA_HOME correctly. Here is how you can accomplish that task with a demonstration of how to set JAVA_HOME in Windows and echo the result to validate that the changes have gone into effect.  Also, let’s explore how you can add Java to the Windows PATH, which is another configuration parameter that is often set after a JDK installation.

How to set JAVA_HOME in Windows

Three ways to set the JDK JAVA_HOME variable in Windows include:

  • allow the Java installer to do it;
  • use the environment variable editor; and
  • use the command line.

OpenJDK’s JAVA_HOME configuration tool

Not every Java installer will automatically set JAVA_HOME for you. The AdoptOpenJDK one will, and it can configure the PATH variable for you as well. But, it doesn’t do it all by default. You must perform a custom install and select one of the following two options to have the OpenJDK installer set JAVA_HOME and PATH environment variables:

  • add to PATH; or
  • set JAVA_HOME variable.

set JAVA_HOME OpenJDK

The OpenJDK JAVA_HOME setup tool greatly simplifies configuration.

That’s all there is to it. Once the OpenJDK installation is complete, the JAVA_HOME variable will be configured and the bin directory of the JDK will be added to the Windows PATH. It doesn’t get much easier than that.

The JAVA_HOME environment variable editor

If your JDK installation didn’t set JAVA_HOME automatically, you can always open the Windows environment variable editor and set it yourself. This is the easiest way to manually set JAVA_HOME in Windows 7, 8 and 10.  

Follow these steps to manually set JAVA_HOME:

  • Open the Windows System Properties Control Panel applet on any version of Windows.
  • Choose Advanced System settings.
  • Click on the Environment Variables button.
  • Click on the New button under System Variables.
  • Set JAVA_HOME as the environment variable name.
  • Set the location of the JDK installation as the environment variable Value.
  • Click OK and close the JAVA_HOME environment variable editor.

Windows 7 JAVA_HOME

How to use the JAVA_HOME environment variable editor in Windows 7.

After you add the new environment variable, close any and all DOS prompts and command windows, because these tools load environment variables only when they first start. If you try to access the JAVA_HOME variable in any windows that were open prior to the change, the variable will come back as null or undefined. But if you open a new command window, scripts that search for JAVA_HOME will run successfully.

The setx JAVA_HOME command

The third way to configure the Windows JAVA_HOME environment variable is to use the command line and invoke the setx command as follows: 

@REM Configure JAVA_HOME in Windows 10 with setx
setx JAVA_HOME -m «C:\_jdk12.0»
>> setx JAVA_HOME command completed successfully 

As you can see, the setx JAVA_HOME approach is relatively simple. Still, command line interface tools can intimidate some people, and this type of manual coding is prone to error. However, when you write scripts to automate the configuration of the environment, the ability to script the process with setx becomes invaluable.

How to echo JAVA_HOME in Windows

After you set JAVA_HOME in Windows, it’s a good idea to verify that the change has persisted. The easiest way to do this is to echo JAVA_HOME in a command prompt or a BASH shell. 

To echo JAVA_HOME in a Windows DOS prompt or command window, you simply bookend the variable with percentage signs:  

@REM How to echo JAVA_HOME in windows
echo %JAVA_HOME%
>> C:\_jdk12.0 

JAVA_HOME Ubuntu

If you use a BASH shell, or if you have set JAVA_HOME in an Ubuntu environment and need to echo JAVA_HOME in Linux, place a single dollar sign before the environment variable: echo $JAVA_HOME.

How to get JAVA_HOME in Windows scripts

The manner in which you get JAVA_HOME within batch files and shell scripts follows the exact same syntax used by the echo command.  Bookend the variable with percentage signs and use that variable within your code just as you would any other scripted variable. 

Scripts that use JAVA_HOME should always be checked to see if the variable exists. If it does not, an appropriate error message will arise. Here is how the Apache Maven project gets JAVA_HOME in its startup script and reports any errors during the process: 

@REM Apache Maven JAVA_HOME Startup Script
@REM ==START JAVA_HOME VALIDATION ==
if not «%JAVA_HOME%»==»» goto OkJHome
for %%i in (java.exe) do set «JAVACMD=%%~$PATH:i»
goto checkJCmd

:OkJHome
set «JAVACMD=%JAVA_HOME%\bin\java.exe»

:checkJCmd
if exist «%JAVACMD%» goto chkMHome

echo JAVA_HOME environment variable is not defined correctly >&2
echo This environment variable is needed to run this program >&2
echo NB: JAVA_HOME should point to a JDK not a JRE >&2
goto error

The JAVA_HOME PATH setting in Windows

The JAVA_HOME and PATH environment variables serve two very different purposes. JAVA_HOME simply points to where Java is installed. If you add something to the PATH variable, it makes it available throughout the entire operating system. Of course, many developers who install Java actually want the runtime universally available, so they set the JAVA_HOME and PATH environment variables at the same time. 

The big distinction between PATH and JAVA_HOME settings is that the former points to the JDK bin directory, while the latter points to the installation directory. Developers are notorious for mixing up these two settings, which invariably leads to program start issues and the subsequent JAVA_HOME error messages such as «java_home is set to an invalid directory» or «java_home environment variable is not set.»

Java PATH variable

How to add the JDK bin directory to the Windows PATH.

The JAVA_HOME bin combination

When you add the JDK \bin directory to the PATH, you can specify the absolute path, or you can get clever and reference the JAVA_HOME environment variable as so: 

%JAVA_HOME% bin

How to set JRE_HOME in Windows

It is worth noting that while a JDK installation is linked to the JAVA_HOME environment variable, JRE installations are typically linked to the JRE_HOME variable. The steps to set up JRE_HOME on a Windows machine are exactly the same as those outlined above, with the exception that the JRE_HOME variable will point to the root of the JRE installation, while the JAVA_HOME environment variable in Windows points to the root of the JDK installation.

In this post, we will see about how to set java path in windows 10 using cmd. To set java path, you need to first understand about JAVA_HOME and how to set JAVA_HOME in windows 10.

Let’s first understand about JAVA_HOME and then we will see how to set JAVA_HOME in windows 10.

What is JAVA_HOME?

JAVA_HOME is environment variable which contains installation directory of Java development kit(JDK) or Java Runtime environment(JRE). This environment variable is setup at operating system level.

JAVA_HOME=C:\Program Files\Java\jdk11.0.9

Why do you need JAVA_HOME?

JAVA_HOME environment variable points to directory where java is installed on your system, so many java based applications such as TOMCAT use JAVA_HOME environment variable to locate java executables.

How to set JAVA_HOME in Windows 10?

  1. Locate JDK on your machine.
    • If you are using 64-bit java, then it will be in C:\Program Files\Java\
    • If you are using 32-bit java, then it will be in C:\Program Files (x86)\Java\
  2. Open windows search, type environment and click on Edit the system environment variables.

    Edit environment variable in java

  3. In System properties dialog, go to Advanced tab and click on button Enviroment Variables.

    Click on Edit environment variable

  4. In System variables, click NEW... button to add JAVA_HOME environment variable.

    Add new environment variable

  5. Provide variable name as JAVA_HOME and value as Java installation directory.

    Set Java home environment variable

Now let’s see how to set java in path environment variable in windows 10.

  1. In System variable window, locate Path and click on Edit....

    Click on edit

  2. Double click on the empty row and add %JAVA_HOME%\bin.

    Set java path in windows 10
    Here, % symbol is used to locate JAVA_HOME environemt variable and \bin provides location for java.exe and h=javac.exe

  3. Close the command prompt and launch again.
  4. Check java version as below:

    Check java version

That’s all about How to set java path in windows 10.

PATH is an environment variable that is used by Operating System to locate the exe files (.exe) or java binaries ( java or javac command). The path once it is set, cannot be overridden. The PATH variable prevents us from having to write out the entire path to a program on the Command Line Interface every time we run it. Moreover, the path is just a variable that stores a bunch of shortcuts.

To execute java console-based programs in windows or Linux environments we have to use java and javac commands. The commands java and javac are not known to the operating system as we don’t specify where the executables reside. Hence, we need to specify the path where the executables are located. This is the reason we set the path and specify the path of the bin folder because the bin contains all binary executable files. After setting the path it can load all necessary items in the program including the compiler or interpreter itself. 

Below is the procedure for setting the path for both Windows and Linux:

Setting Java Path in Windows

1. Go to the Search box and type advanced system settings in it. Now click on the View advanced system settings.

2. Select the Advanced tab and then click environment variables.

3. In the system, variables click the New button. Now in the edit system variable, type variable name as JAVA_HOME and variable path as the path where the JDK folder is saved and click on OK button Usually the path of the JDK file will be C:\Program Files\Java\jdk1.8.0_60.

4. Now in the system variables go to the path and click the edit button.

5. Click the New button.

6. Now add the following path: %JAVA_HOME%\bin

Setting Java Path in Linux

  • Open the terminal and enter the following command: 
sudo nano /etc/environment.
  • A file will be opened and add the following command to that file:
JAVA_HOME = "YOUR_PATH". 
  • Replace YOUR_PATH with the JDK bin file path.
  • Now restart your computer or virtual machine that you are using (or) reload the file: source /etc/environment
  • You can test the path by executing
 echo $JAVA_HOME
  • If you get the output without any error, then you’ve set the path correctly.
  • If you get any errors, try repeating the procedure again.

Last Updated :
08 Sep, 2022

Like Article

Save Article


Download Article

The easiest ways to set the JAVA_HOME environment variable on any computer


Download Article

  • Windows
  • |

  • macOS
  • |

  • Linux
  • |

  • Video
  • |

  • Tips

Are you seeing Java errors like «JAVA_HOME is not defined correctly?» or «JAVA_HOME is set to an invalid directory?» If you’ve recently installed the Java Development Kit (JDK) or the Java Runtime Environment (JRE), you’ll need to set your JAVA_HOME variables and configure the path so applications know where to find Java. This wikiHow article will show you the easiest ways to change or set the Java home path on Windows, macOS, and Linux.

Things You Should Know

  • Before you can set JAVA_HOME, you’ll need the full path to your JDK or JRE installation.
  • Once you set the JAVA_HOME environment variable, you can run the command echo $JAVA_HOME to see the new path.
  • To set the Java home and path on Linux or macOS permanently (even after a reboot), add the environment variables to your .bashrc or .zshrc file.
  1. Image titled Set Java Home Step 1

    1

    Find your Java installation directory. Assuming that you didn’t change the path for the JDK or JRE during installation is not changed, it should be in a directory under C:\Program Files\Java. There are a few ways to find the full path:

    • Open File Explorer, click This PC in the left panel, then navigate to C:\Program Files\Java. The directory you’re looking for should have the name of the JDK version, such as C:\Program Files\Java\jdk-19.
    • If you installed the JRE instead of the JDK, you’ll have something like C:\Program Files\Java\jre1.8.0_351 instead.
    • You can also open the command prompt and run the command wmic product where "Name like '%%Java%%'" get installlocation,Name. This will tell you the full path of the JDK, even if you haven’t yet set JAVA_HOME.
  2. Image titled Set Java Home Step 2

    2

    Open your Advanced System Settings. Here’s how:

    • Press the Windows key on your keyboard and type advanced system.
    • Click View advanced system settings in the search results.

    Advertisement

  3. Image titled Set Java Home Step 3

    3

    Click the Environment Variables button. You’ll see it at the bottom-right corner of the System Properties window.

  4. Image titled Set Java Home Step 4

    4

    Click the New… button under «User variables.» You’ll have two sections on this window—»User variables» and «System variables.» You’ll want to make sure you’re clicking New under the first box (User variables).

    • If you have multiple installations of the JDK and want to change JAVA_HOME to your new installation, select the current JAVA_HOME user variable and click Edit… instead.
  5. Image titled Set Java Home Step 5

    5

    Type JAVA_HOME as the «Variable name.»

    • If you’re editing the current JAVA_HOME path, you’ll already have JAVA_HOME here. So, you can skip this step.
  6. Image titled Set Java Home Step 6

    6

    Enter the full path to your JDK or JRE installation. For example, if the path is C:\Program Files\Java\jdk-19, enter that full path into the «Variable value» field.[1]

    • If you’re adding a second path to JAVA_HOME, just type a semicolon (;) after the first path, then enter the second path.
    • If you’re replacing an old JAVA_HOME path, just delete the current path and enter the new one.
  7. Image titled Set Java Home Step 7

    7

    Click OK. This takes you back to the Environment Variables window.

  8. Image titled Set Java Home Step 8

    8

    Select the Path user variable and click Edit. You should already have a variable in the «User variables» section called «Path.» To make sure you can run the Java binaries, you’ll need to edit your path to add the location of the JDK bin directory.

    • If you want other users on this PC to be able to access Java binaries from the command line, repeat this step for the «Path» variable under «System variables» as well.
  9. Image titled Set Java Home Step 9

    9

    Append the path to your Java /bin directory to the end of the path. You’ll already have at least one directory in your path, which needs to stay in place. Here’s how to add the Java bin directory:

    • Click the New button at the top.
    • Enter the full path to the JRE or JDK with \bin at the end. For example, C:\Program Files\Java\jdk-19\bin or C:\Program Files\Java\jre1.8.0_351\bin.
    • Click OK.
  10. Image titled Set Java Home Step 10

    10

    Click OK on all subsequent windows. This updates your JAVA_HOME variable and adds the Java binaries to your path.

    • You won’t need to restart your computer for the changes to take effect, but you will need to relaunch any apps that were trying to access Java.
    • Open a new command prompt window and run the command echo %JAVA_HOME% to display the new path to JAVA_HOME.
      • Make sure this is a new command prompt window. If you still have the same window open, the command will fail because it doesn’t have the new environment variables.
  11. Advertisement

  1. Image titled Set Java Home Step 11

    1

    Open a Terminal window. To open Terminal, search for Terminal in Spotlight. Or, you can open Finder, click the Go menu, select Utilities, and choose the Terminal app.

  2. Image titled Set Java Home Step 12

    2

    Type /usr/libexec/java_home and press Return. This handy utility that comes preinstalled on macOS will tell you the full path to your Java installation, which you’ll need to set JAVA_HOME.

    • If you have more than one Java installation and want to see the paths to all of them, use /usr/libexec/java_home -V instead.
  3. Image titled Set Java Home Step 13

    3

    Copy the path and paste it somewhere handy. Highlight the path to the Java installation you want to use as JAVA_HOME, press Cmd + V to copy it, then paste it into a sticky note or text file.

  4. Image titled Set Java Home Step 14

    4

    Open your .zshrc file in a text editor. As long as you’re using macOS Catalina or later, you’ll be using the zsh shell by default, which means you’ll add JAVA_HOME and the path to your .zshrc file.[2]
    If you’re using an earlier version of macOS (or prefer the bash shell), replace .zshrc with .bashrc in these commands instead. Here’s the easiest way to open the file:

    • Type cd ~ and press Return.
    • Type open .zshrc and press Return. This should open the file in a text editor.
    • If the file is not found, type echo > .zshrc and press Return. Then, run open .zshrc again.
  5. Image titled Set Java Home Step 15

    5

    Add these lines to the bottom of the file. Adding these lines ensures that JAVA_HOME will be set correctly even after you log off or open another window. If the file has existing contents, just scroll down past them to enter the following text on individual lines:

    • export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home

      • Replace /Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home with the full path to the /Contents/Home directory of your Java installation if it’s different.
    • export PATH=$JAVA_HOME/bin:$PATH
    • Save the file and exit the text editor when you’re finished.
  6. Image titled Set Java Home Step 16

    6

    Type source .zshrc and press Return. Once you’ve edited your profile, this command ensures that your environment variables will be updated for the current terminal window (and any other windows you open from now on).

  7. Image titled Set Java Home Step 17

    7

    Type echo $JAVA_HOME and press Return to confirm JAVA_HOME. You should now see the full path to JAVA_HOME.

    • If you had any other windows open that were attempting to find Java binaries, close and reopen them.
  8. Advertisement

  1. Image titled Set Java Home Step 18

    1

    Open a terminal window. If you’re logged in to your desktop environment, you can press Ctrl + Alt + T to launch a terminal window.

    • This method will work in just about any version of Linux, including Ubuntu and Redhat.
  2. Image titled Set Java Home Step 19

    2

    Find the location of your Java installation. One way you might find the Java install location is to run the which javac command, but this will only give you a symbolic link. Instead, run this command to find the location of the executable file:

    • readlink -f `which javac`
    • If that doesn’t work, try running update-alternatives --list java.
    • If neither of these commands works, run whereis java, which will usually result in a symbolic link like /usr/bin/java.
      • Once you get the directory, find out where it links using ls -la /bin/java.
      • If that points you to another directory, e.g., /etc/alternatives/java, run ls -la /etc/alternatives/java.
      • At that point, you should see a much longer directory, which is actually the home to the Java binaries. For example, usr/lib/jvm/java-11-openjdk-arm64/bin/java. This is the directory you want.
  3. Image titled Set Java Home Step 20

    3

    Set the JAVA_HOME path. Now that you have the location of Java, you can add the path to your .bashrc file (if you’re using the bash shell), .zshrc (for the zsh shell), or the equivalent for your shell.[3]
    Run both of the following commands, replacing the path with the path to your JDK or JRE installation, and «bashrc» with your shell’s login script:

    • echo "export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-arm64" >> ~/.bashrc
    • echo "export PATH=$PATH:$JAVA_HOME/bin" >> ~/.bashrc
  4. Image titled Set Java Home Step 21

    4

    Type source .bashrc to apply the changes to the current terminal window. Though you’ve already set the home for Java and added the path to its binaries permanently, the changes won’t take effect in your current terminal window until you run this command.

    • At this point, you can run echo $JAVA_HOME to see the location of Java on your system.
  5. Advertisement

Ask a Question

200 characters left

Include your email address to get a message when this question is answered.

Submit

Advertisement

Video

  • In Linux, you can set JAVA__HOME for all users by editing the global .bashrc, which is located at /etc/bash.bashrc. Just use echo and replace ~/.bashrc with /etc/bash.bashrc.

  • On both Linux and macOS, you can set the Java home in your .bash_profile or .zprofile if you prefer.

Thanks for submitting a tip for review!

Advertisement

About This Article

Thanks to all authors for creating a page that has been read 334,138 times.

Is this article up to date?

  • Windows launcher скачать на телефон
  • Windows keyboard layout creator windows 10
  • Windows iso скачать бесплатно торрент iso на русском языке
  • Windows is loading files долго загружается с флешки
  • Windows launch exe from command line