Wednesday, May 13, 2020

How To Add Java Jdk To Path

How_To_Add_Java_Jdk_To_Path

In the previous tutorial, we have learned how to install java JDK both on windows or linux system. However there are a few settings that should be done to help us in running a Java program code. Now, we will learn how to add java JDK to path on Windows and Linux systems.

Java JDK Path Windows 10


According to the Oracle documentation, it is explained that we can actually run java without the need to set environment paths. However, if you don't set the java JDK path variable, then you need to specify the full address of the variable path every time yourun a java program, for example:

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

In general, path is the term for addressing a file in the computer's directory. For example we have a learn_java.doc document. The path address can be stored in C:\MyDocument\learn_java.doc.

So, if we say "set the path in the Windows operating system", then that means we input a folder address so that Windows cmd can run stored applications from anywhere.

For example, the java.exe and javac.exe files that will be used to process Java languages are in the C:\Program Files\Java\jdk1.8.0_191\bin\folder. So every time we need to access this file, we must open the folder C:\Program Files\Java\jdk1.8.0_191\bin first.

By setting the path address, later java.exe and javac.exe files can be accessed from any address.

To better understand, let's open Windows cmd:

default_windows_cmd

By default, the current directory is C:\Users\Erlangga, that is to say, currently Windows cmd is in the Users\Erlangga folder on drive C. The position you find can be different because here "Erlangga" is my username.

All typed cmd commands will only affect the C:\Users\Erlangga folder. For example, type java.exe -version then press Enter:

cmd-windows-error-java.exe-not-found

The result is:

'java.exe' is not recognized as an internal or external command, operable program or batch file.

This error message appears because I tried to access the java.exe file in the C:\Users\Erlangga folder. Of course the java.exe file was not found because the location of java.exe is in C:\Program Files\Java\ jdk1.8.0_191\bin\java.exe.

By setting the system path in Windows, we can tell Windows cmd to search for the java.exe file in the C:\Program Files\Java\ jdk1.8.0_191\bin folder automatically.

Changing the path settings can be different depending on the version of Windows. The steps below are for Windows 10.

First open the System Properties settings window. The fastest way is to click the search icon next to the Start Menu (Windows Logo), then type "environment". The "Edit the system environment variables" option will appear.
access_menu_system_environtment

Another way is through the Control Panel, select System and Security, select the System menu, then on the left menu click "Advanced System Settings".

advanced_system_settings_control_panel

Then select the "Advanced" tab in the "System Properties" window. Then, at the bottom right, click the environment variable button:

environment_variable_setup_path

In the Environment Variable window, select the Path line at the bottom, then click the Edit button.

edit_environment_variable_path

The Edit Environment Variable window will now appear as follows:

new_edit_environment_variable_path

Click the New button at the top right. The cursor will switch to the lower left side, then type the address of the Java SDK path, which is C:\ Program Files\Java\jdk1.8.0_191\bin\. If you store Java JDK at another address, adjust the writing path.

add_jdk_path_environment_variable

After the path C:\Program Files\Java\jdk1.8.0_191\bin\ is in the table, click OK to end, then click the OK button several times to close all Environment Variable and System Properties windows.

Check using the java.exe -version command:

check_java_version_windows

Java JDK Path Linux (Ubuntu)

Open the file /etc/environment with any text editor. In this example, I use Nano:

$ sudo nano /etc/environment

Add the following line at the end to set the JAVA_HOME value:

JAVA_HOME="/usr/lib/jvm/jdk1.8.0_191/bin/java"

NOTE: Don’t forget to update it with the actual path to your Java installation.

Press CTRL+X to finish editing and save the changes. Next, make the changes by applying this command:

source /etc/environment

You can double-check if it’s active by entering:

echo $JAVA_HOME

check_java_jdk_path

That is, we have learned how to add java JDK to path in either Windows or Linux. This technique is not only useful for Java applications, but also for many other applications.