Table of Contents

    Setting Path in Java: Complete Guide and Best Practices

    Setting Path in Java: Complete Guide and Best Practices

    How to set path in Java

    PATH is an environment variable which is used to locate JDK binaries like "java" or "javac" command used to run java program and compile java source file. PATH can not be overridden by any Java settings.PATH environment variable is used by operating system to find any binary or command typed in the shell, this is true for both Windows and Linux environment

    If you are saving the java source file inside the jdk/bin directory, path is not required to be set because all the tools will be available in the current directory. But If you are having your java file outside the jdk/bin folder, it is necessary to set path of JDK.

    There are 2 ways to set java path:

    1. Temporary Path
    2. Permanent Path

    How to set Temporary Path of JDK in Windows

    To set the temporary path of JDK, you need to follow following steps:

    • Step 1: Open command prompt
    • Step 2: copy the path of jdk/bin directory
    • Step 3: write in command prompt: set path=copied_path

    For Example:

    set path=C:\Program Files\Java\jdk1.6.0_23\bin
    temporary path in java

    How to set Permanent Path of JDK in Windows 10

    Step 1 : To set path, search "Environment Variables", click "Edit the system environment variables".

    Edit the system environment variable

    Step 1 : Click on "Environment Variables..."


    Step 1 : Select and edit Path

    Edit the system environment variable

    Step 1 : Add the path of the bin folder of your JDK, "C:\Program Files\Java\jdk1.8.0_60\bin" in my case.

    Edit the system environment variable

    Step 1 : After path set, you can run javac in command prompt.

    Edit the system environment variable

    Setting Java Path in Linux OS

    Setting the path in Linux OS is same as setting the path in the Windows OS. But here we use export tool rather than set. Let's see how to set path in Linux OS:

    export PATH=$PATH:/home/jdk1.6.01/bin/

    Here, we have installed the JDK in the home directory under Root (/home).