Navigation

Setting Java PATH System Variable

Setting Java PATH System Variable

1) What is PATH?

The PATH is the system variable that your operating system uses to locate needed executables from the command line or Terminal window.

The PATH system variable can be set using System Utility in control panel on Windows, or in your shell's startup file on Linux.

The PATH environment variable is a series of directories separated by semicolons (;).

The following is an example of a PATH environment variable:

C:\Java\jdk1.7.0\bin;C:\Windows\System32\

2) Why we need to set PATH?

Set the PATH environment variable if you want to be able to conveniently run the executables (javac.exe, java.exe, javadoc.exe, and so on) from any directory without having to type the full path of the command. If you do not set the PATH variable, you need to specify the full path to the executable every time you run it, such as:

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

Above command helps to compile “MyClass.java” file.

3) Setting Java PATH.

3.1) Setting PATH on Windows.

Windows 8

  1. Drag the Mouse pointer to the Right bottom corner of the screen.

  2. Click on the Search icon and type: Control Panel.

  3. Click on -> Control Panel -> System -> Advanced ->Environment Variables.

  4. Edit or Create PATH Environment Variable (EV),

    • If “Path” Environment Variable is already present under System Variables, then click Edit.

    • If “Path” Environment Variable is not present under System Variables, then Click New under System Variable and provider Variable name as “path”.

    Image selects PATH variable from Environment Variable Dialog.
  5. Include Java installation directory in PATH

    • Edit or create the appropriate PATH environment variable (as above)

    • Append the location of Java bin directory of the JDK installation, in my case it is "D:\Program Files\Java\jdk1.7.0_11\bin" to the end of the existing path:

      %SystemRoot%\system32;[...];D:\Program Files\Java\jdk1.7.0_11\bin

    • Note that paths are separated from each other with semicolons (;)

    Image Edits PATH variable in Edit System Variable Dialog.
  6. Close the window.

  7. Reopen Command prompt window, and run your java code.

Windows 7

  1. Select Computer from the Start menu.

  2. Choose System Properties from the context menu.

  3. Click Advanced system settings > Advanced tab -> Environment Variables.

  4. Edit or Create PATH Environment Variable (EV),

    • If “Path” Environment Variable is already present under System Variables, then click Edit.

    • If “Path” Environment Variable is not present under System Variables, then Click New under System Variable and provider Variable name as “path”.

    Image selects PATH variable from Environment Variable Dialog.
  5. Include Java installation directory in PATH

    • Edit or create the appropriate PATH environment variable (as above)

    • Append the location of Java bin directory of the JDK installation, in my case it is "D:\Program Files\Java\jdk1.7.0_11\bin" to the end of the existing path:

      %SystemRoot%\system32;[...];D:\Program Files\Java\jdk1.7.0_11\bin

    • Note that paths are separated from each other with semicolons (;)

    Image Edits PATH variable in Edit System Variable Dialog.
  6. Close the window.

  7. Reopen Command prompt window, and run your java code.

3.2) Setting PATH on Linux Based Bash Profile System (including Mac).

Set PATH for Single User

  1. Login to your account and open “.bash_profile” file using below command.

    $ vi ~/.bash_profile

  2. Above command opens “.bash.profile” file in vim editor.

    .bash.profile opens in vm editor.

    Refer below URL to know how to edit a file in vim editor.

    http://www.radford.edu/~mhtay/CPSC120/VIM_Editor_Commands.htm

  3. Set PATH by adding below line in your “.bash.profile” file using vim editor.

    export PATH=$PATH:/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home/bin

    Feel free to replace “/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home/bin” as per your setup. Save and close the file.

  4. Just logout and login back to see new changes. Alternatively, type the following command to activate the new path settings immediately:

    $ source ~/.bash_profile

    Or

    $ . ~/.bash_profile

Set PATH for All User

  1. You need to setup global config in “/etc/profile” OR “/etc/bash.bashrc” file for all users:

    # vi /etc/profile

  2. Above command opens “/etc/profile” file in vim editor.

    etc/profile file opens in vim editor.

    Refer below URL to know how to edit a file in vim editor.

    http://www.radford.edu/~mhtay/CPSC120/VIM_Editor_Commands.htm

  3. Set PATH by adding below line in your “/etc/profile” file using vim editor.

    export PATH=$PATH:/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home/bin

    Feel free to replace “/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home/bin” as per your setup. Save and close the file.

  4. Just logout and login back to see new changes. Alternatively, type the following command to activate the new path settings immediately:

    $ source ~/.bash_profile

    Or

    $ . ~/.bash_profile

4) Check If Java PATH has been Successfully Set.

  1. Open Command Prompt (Windows) or Terminal in (Mac).

  2. Run the below command.

    java -version

  3. Above command should print version of java that has been installed in your machine., if PATH has been successfully set.

    If Java path is successfully set then the Java Version will be printed in command line.

No comments:

Post a Comment