Java installation and setting environment variables

How to set environment variables for Java on your System? : Help Center

How to set Environment variables for Java JDK on your machine?

If you want to execute Java programs, you need to install Java Development Kit(JDK) or Java Runtime Environment(JRE) and include the Java Executable location to the Path Environment Variable. Modern Java installers do this automatically.

How to check if Java is already installed and the Path variable has been set?

You can manually check if Path variable has been set once the installation is complete by following this simple method:

1. Open Command Prompt(Windows) or Terminal(Mac/Linux) window.

2. Enter `java -version` and press Enter.

If this command returns a version number, Java is installed and the Path environment has been set. Otherwise, an error message will be shown and you might need to reinstall Java or set the Path variable as given below.

Note: You may either set the User-level Path variable or the System-wide Path variable. If the System-wide Path variable is set, you won’t have to do the same for other User Accounts on the machine.

Читайте также:  Javascript обновить страницу при клике

What are Environment Variables — Path, JAVA_HOME, and JRE_HOME?

JAVA_HOME — This variable points to the base directory of the JDK installation in your system. This is the folder which contains the ‘bin’ and ‘lib’ folders for Java.

JRE_HOME — This variable points to the base directory of the JRE installation in your system.

Path — The Path Environment variable helps the system to determine search path for commands. When a command is executed, the system will search in these locations for an executable file with the name of the command. Therefore, if we need to run Java programs from anywhere in the System, we need to add the «Java binary folder location» to the Path variable.

Note: Path variable will be automatically set by the Java Installer.

Finding Java Installation location

For Windows:

Generally, t he java_installation_base_folder location is C:\Program Files\Java\jdk

For 64 bit machines, t he java_installation_base_folder location is C:\Program Files (x86)\Java\jdk or the above one.

For Linux:

For Linux, you might be using either OpenJDK or Oracle JDK. You can use the whereis java command to find java executable location on your Linux machine.

For OpenJDK, The java_installation_base_folder location is similar to /usr/lib/jvm/openjdk/jdk

For Oracle JDK, The java_installation_base_folder location is similar to /usr/lib/jvm/java-8-oracle/

For Mac:

You can use /usr/libexec/java_home command to find java installation folder on your Mac.

Note: There might be slight changes in your installation location according to the version of Java installed and your OS version. That’s why it is recommended to check the java installation location before setting the Path variable.

Setting the System Environment Variables

For Windows:

After getting the installation location using the where command,

1. Open Command prompt(CMD) program with Administrator privileges.

2. Enter the following command with your java base directory location and press Enter key.

setx /m JAVA_HOME "" setx /m JDK_HOME "" setx /m JRE_HOME "\jre"

Check setx /? for more help.

For Java 8 on Windows 10, we have found that the java installation location is «C:\Program Files\Java\jdk1.8.0_144»

Therefore the format of the setx command would be,

setx /m JAVA_HOME "C:\Program Files\Java\jdk1.8.0_144" setx /m JDK_HOME "C:\Program Files\Java\jdk1.8.0_144" setx /m JRE_HOME "C:\Program Files\Java\jdk1.8.0_144\jre"

For Mac/Linux:

After getting the installation location using the whereis or locate command,

1. Open Terminal window and type the following command to open the .profile file in nano editor

This will open the .profile file in the terminal window.

2. Scroll down to the end of the file and add the following line at the end.

export JAVA_HOME= export JDK_HOME= export JRE_HOME=/jre
export JAVA_HOME=/usr/java/jdk1.5.0_07 export JDK_HOME=/usr/java/jdk1.5.0_07 export JRE_HOME=/usr/java/jdk1.5.0_07/jre

3. Press Ctrl+O to write it to the file and confirm the changes.

Note: For Mac, you may need to enable locate command by executing the following command in Terminal.

sudolaunchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist

Here’s another detailed article from java.com on setting up the Environment variables for Java in Windows/Mac/Linux — How do I set or change the PATH system variable?

Modified on: Fri, 29 Mar, 2019 at 1:56 PM

Did you find it helpful? Yes No

Источник

Setting up the environment in Java

Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented, etc. Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture. The latest version is Java 19. Below are the environment settings for both Linux and Windows. JVM, JRE, and JDK three are all platform-dependent because the configuration of each Operating System is different. But, Java is platform-independent. Few things must be clear before setting up the environment which can better be perceived from the below image provided as follows:

  • JDK(Java Development Kit): JDK is intended for software developers and includes development tools such as the Java compiler, Javadoc, Jar, and a debugger.
  • JRE(Java Runtime Environment): JRE contains the parts of the Java libraries required to run Java programs and is intended for end-users. JRE can be viewed as a subset of JDK.
  • JVM: JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides a runtime environment in which java bytecode can be executed. JVMs are available for many hardware and software platforms.

Now let us discuss the steps for setting up a Java environment with visual aids. Let’s use the Windows operating system to illustrate visual aids.

Steps: Here we will be proposing steps for three different operating systems as listed:

A. Windows operating systems

The steps for setting the environment in the Windows operating system are as follows:

Step 1: Java8 JDK is available at Download Java 8. Click the first link for Windows(32-bit) and the last link for Windows(64-bit) as highlighted below.

Step 2: Now you will see a download prompt just accept the term and conditions and click on the download button

Step 3: If you have an oracle account then sign in or if don’t then create one and sign in

Step 4: Then your download will start automatically after signing in if don’t then click on the previous link again

Step 5: After downloading, run the jdk-8u Application(.exe file) and follow the instructions to install Java on your machine. Once you install Java on your device, you have to set up the environment variable.

Step 6: Go to Control Panel -> System and Security -> System. Under the Advanced System Setting option click on Environment Variables as highlighted below.

Step 7: Now, you have to alter the “Path” variable under System variables so that it also contains the path to the Java environment. Select the “Path” variable and click on the Edit button as highlighted below.

java environment setuo

Step 8: You will see a list of different paths, click on the New button, and then add the path where java is installed. By default, java is installed in “C:\Program Files\Java\jdk\bin” folder OR “C:\Program Files(x86)\Java\jdk\bin”. In case, you have installed java at any other location, then add that path.

Java environment setup

Step 9: Click on OK, Save the settings, and you are done !! Now to check whether the installation is done correctly, open the command prompt and type javac -version. You will see that java is running on your machine.

Note: To make sure whether the compiler is set up, type javac in the command prompt. You will see a list related to javac.

B. Linux Operating System

In Linux, there are several ways to install java. But we will refer to the simplest and easy way to install java using a terminal. For Linux, we will install OpenJDK. OpenJDK is a free and open-source implementation of the Java programming language. Steps for setting the environment in the Linux operating system are as follows:

Step 1: Go to Application -> Accessories -> Terminal.

Step 2: Type the command as below as follows:

sudo apt-get install openjdk-8-jdk

Step 3: For the “JAVA_HOME” (Environment Variable) type the command as shown below, in “Terminal” using your installation path…(Note: the default path is as shown, but if you have to install OpenJDK at another location then set that path.)

export JAVA_HOME = /usr/lib/jvm/java-8-openjdk

Step 4: For “PATH” (Environment Value) type command as shown below, in “Terminal” using your installation path…Note: the default path is as shown, but if you have to install OpenJDK at another location then set that path.)

export PATH = $PATH:/usr/lib/jvm/java-8-openjdk/bin

Note: We are done setting up the environment in Java for Linux OS.

  • Notepad/gedit : They are simple text-editors for writing java programs. Notepad is available on Windows and gedit is available on Linux.
  • Eclipse IDE : It is the most widely used IDE(Integrated Development Environment) for developing software in java. You can download Eclipse.

C. MacOS Operating System

Step 1: Open the terminal from the application folder or simply press the “command” and “shift” key together and write initials of the terminal and press enter.

It will be good to have package manager such as homebrew installed in your machine as we can operate to install any software from here itself simply by using terminal commands.

Step 2: Now in order to configure first write the command ‘java –version ‘ where the message below it will pop that there is no

java --version javac --version

Note: If it was set up then you would have been getting the version displayed on the screen as it is shown below where in that machine it was already set up. So remember to cross-check in your machine once you have successfully set it up in yours.

Step 2: Once we are done with installing JDK now let us move on setting up the java_home environment variable for that you will have to look into something called s ‘bash_profie’ using the below command

You will notice that in your terminal there will be no bash_profile set but it is shown below so here in this machine it is already set up. In order to set it up if not there we have to create it which lets us prior seek into java home variables whether it is set up or not.

Step 3: Setting up the home java variable. Use the below command to check or setup if not installed as follows on the terminal:

If it is showing blank then the java home variable is not set up as perceived from the above image.

Step 4: Installing bash_profile

Make sure to go to the root folder in the terminal and write the command ‘touch ./bash_profile ‘

Now you will see that bash-profile s created which is as shown in the step2 in your machine which hone can verify by writing command as follows:

Step 5: Edit the .bash_profile created for java, you just have to write the command marked in below media and provided below as follows:

export JAVA_HOME=$(/usr/libexec/java_home) // No need to remember this command

Save this file and relaunch the terminal by closing it.

Step 5: Verifying whether it is installed by entering the following two commands

source .bash_profile echo $JAVA_HOME

From the above media, we can see the java variable is all set to go as earlier there was a blank therein the above media.

Источник

Оцените статью