Setting environment variables in java windows

Environment Variables

Many operating systems use environment variables to pass configuration information to applications. Like properties in the Java platform, environment variables are key/value pairs, where both the key and the value are strings. The conventions for setting and using environment variables vary between operating systems, and also between command line interpreters. To learn how to pass environment variables to applications on your system, refer to your system documentation.

Querying Environment Variables

On the Java platform, an application uses System.getenv to retrieve environment variable values. Without an argument, getenv returns a read-only instance of java.util.Map , where the map keys are the environment variable names, and the map values are the environment variable values. This is demonstrated in the EnvMap example:

import java.util.Map; public class EnvMap < public static void main (String[] args) < Mapenv = System.getenv(); for (String envName : env.keySet()) < System.out.format("%s=%s%n", envName, env.get(envName)); >> >

With a String argument, getenv returns the value of the specified variable. If the variable is not defined, getenv returns null . The Env example uses getenv this way to query specific environment variables, specified on the command line:

Passing Environment Variables to New Processes

When a Java application uses a ProcessBuilder object to create a new process, the default set of environment variables passed to the new process is the same set provided to the application’s virtual machine process. The application can change this set using ProcessBuilder.environment .

Platform Dependency Issues

There are many subtle differences between the way environment variables are implemented on different systems. For example, Windows ignores case in environment variable names, while UNIX does not. The way environment variables are used also varies. For example, Windows provides the user name in an environment variable called USERNAME , while UNIX implementations might provide the user name in USER , LOGNAME , or both.

To maximize portability, never refer to an environment variable when the same value is available in a system property. For example, if the operating system provides a user name, it will always be available in the system property user.name .

Источник

Java Environment Variables

Java Environment Variables

  1. What Are Environment Variables and Why We Use It
  2. Set Environment Variables for Java in Windows
  3. Set Environment Variables for Java in macOS and Linux

Today, we will look at an important topic that can be helpful when we install the JDK or Java Development Kit in an operating system. Environment variables usually describe the environment and how things should behave in this environment. There are various variables available, but the most used variable is PATH. We will look deeply into this topic in the following sections.

What Are Environment Variables and Why We Use It

Environment Variables are key/value pairs that specify configurations for various programs in an operating system. Most of the popular operating systems like Windows, macOS, and Linux use it. These values are commonly Strings.

Читайте также:  Java boolean operators and or

The common use of these variables is when we want to execute a program using a command without going to or writing the whole path to the program. For example, if we want to run a Java program anywhere, w6e may have to first go to the location where Java is located through the command-line or terminal, and then we can use Java.

But if we specify the path to Java in the environment variables, we have to use the program’s name or the command attached to it, and the program will be executed.

Set Environment Variables for Java in Windows

We can install Java or JDK in Windows using the install wizard that comes with it, but we have to set the environment variables manually. In the Windows operating system, we can access the settings for environment variables, edit them, or use the command line.

We will see the steps to set the variables using the command line.

First we get the location where Java is installed or the Java that we want to use globally. We can do this easily by using the following command in the command line.
C:\Users\User1\.jdks\openjdk-15.0.1\bin\java.exe 
Now that we have the path, we use it to set the variables; there are two variables we need to configure that are the JAVA_HOME and PATH variables. To do this, we open the command line or CMD for short, and it is better to run it as administrator.
In the command line, we write the following command to set both variables:
setx JAVA_HOME "C:\Users\User1\.jdks\openjdk-15.0.1" setx PATH "C:\Users\User1\.jdks\openjdk-15.0.1\bin\"; 

It will set or update the variables. Note that JAVA_HOME uses the path where the whole Java folder is situated, while PATH is the path of the subfolder called \bin where Java.exe and other executables are located.

Set Environment Variables for Java in macOS and Linux

In the macOS and Linux, we can use the following steps to set or update the environment variables for Java:

Open the terminal
identify the default shell that is being used by the OS using this command:

This will print the default shell. Note that different versions of the operating system may have different shells; for this tutorial, we use the zsh shell.

Open ./zshenv that is used to set environment variables in the zsh shell using the below command:
Now add the following content.
export JAVA_HOME=$(/usr/libexec/java_home) 
Now to execute the script or the content that we just added, we use the below command:
At last, to check if the variable was set, we will print JAVA_HOME .
/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home 

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

Related Article — Java Environment

Источник

How to set environment variables for Java using command line

Well, in this article I talked about the JVM, JRE and JDK, which are the cornerstones of the Java programming language. You develop Java applications by using tools like editors, IDEs and servers. These tools need to use the Java compiler ( javac ) and Java launcher ( java ) to compile and run Java applications, but these tools don’t know where the JRE or JDK is.

Читайте также:  Java net socket timed out

So, by setting up the environment variables, you tell your tools that:

“Hey, you can find the compiler and launcher here and there”.

The first thing you need to do after installing the JDK is creating an environment variable named JAVA_HOME and then update the PATH variable.

  • JAVA_HOME : stores location of the JDK’s installation directory. When you install development tools, they will first check for the JAVA_HOME variable. If found, they will stick with it. If not, they may ask you to manually specify the location where JRE/JDK is installed.
  • PATH : stores paths of directories where the operating system will look, to launch the requested programs quickly. For Java development, you should update this environment variable by adding an entry to the bin directory under JDK’s installation directory.
  • JAVA_HOME = C:\Program Files\Java\jdk1.8.0
  • PATH = PATH + C:\Program Files\Java\jdk1.8.0\bin

But that isn’t cool, because I’m about to show you how to do the same thing using command line prompt as shown below (Windows 7, 8 and 10):

setx JAVA_HOME "C:\Program Files\Java\jdk1.8.0" setx PATH "%PATH%;%JAVA_HOME%\bin";

The setx command permanently updates the environment variables. To add/update system environment variables, you must use the -m switch and open the command prompt using Administrator privilege: Click Start, type cmd . When the cmd.exe icon appears, right click and select Run as administrator.

To add/update system environment variables:

setx -m JAVA_HOME "C:\Program Files\Java\jdk1.8.0" setx -m PATH "%PATH%;%JAVA_HOME%\bin";

I prefer setting the environment variables using this command-line alternative. It’s quick and easy instead of going through several dialogs like using the GUI.

To summary, here are some important points:

  • Always set JAVA_HOME when preparing development environment for Java.
  • JAVA_HOME points to the installation directory of JDK.
  • The PATH variable should contain an entry pointing to JAVA_HOME\bin .
  • Environment can be set either via the GUI or command line prompt.

About the Author:

Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.

Источник

Step by Step Java Environment Variables Setup [JDK Installation Guide]

Java is widely used in many applications and also provides computing power for numerous websites. In a previous article, we have seen the introduction and features of Java. In this post, I share a complete JDK installation guide and Java environment variables Setup.

To run the Java application, there is a Java Development Kit (JDK) to install. And there are two environment variables to set. One is a path and the other is a classpath variable.

Install JDK on Windows

First of all, download the latest JDK from its official website of Oracle.

Install downloaded JDK software on your system.

Note: In the case of Windows, by default, Java will be installed at directory path C:\Program Files\Java .

After installation opens this directory path. You can find, there are two folders (jdk1.x… and jre ).

JDK folder Hierarchy

Here is a complete JDK file hierarchy inside the Java folder.

Java Environment Variables Setup Steps

After installation, we need to set path and classpath environment variable for Java.

Читайте также:  Add class to div php

Follow the steps on your Windows system as given below.

how to set path in java

  • Right-click on My Computer.
  • Select Advanced System Setting.
  • Click on Environment Variables (at the bottom right corner).
  • There will be two environment variables, one is the User variable and another is a System variable.
  • If path and classpath variables are already present in System Variable, click on it to edit. Otherwise, set a new variable.
  • Set the path variable by adding the following directory path.
C:\Program Files (x86)\Java\jdk1.7.0_67\bin

set path environment variable

Set the classpath variable by adding the following classpath directory path.

C:\Program Files (x86)\Java\jre7\lib

set classpath environment variable

Note: These are the respective JDK paths on my system. Find the same on your system.

  • Click on OK.
  • All the screenshots have taken on Windows OS for installation of Java JDK 7.

    Now you have done with Java JDK setup. You are ready to run your first “Hello World” java program.

    Let’s discuss some frequently asked questions… The questions, everyone should know especially if you are a beginner.

    How to set up the environment variable for a Particular User?

    You can see there are two types of environment variables – User Variable and System Variable.

    • If you want to set java path and classpath for the particular user on a Window system, change path and classpath in User variables.
    • If you want to set Java for all the users on the system, modify path and classpath in the System variable.

    By setting user variables, the only particular user is able to run a Java program. Other users on the system can not access the Java installation file to run the Java program.

    If you want all users should be able to run Java application, set an environment variable in the System Variable. After this, all the user can run a Java program.

    Why do you need to Set Path and Classpath Environment variable in Java?

    Many programmers, knows how to set path in Java. But very few know why there is a need for Java environment variables setup.

    If you have not set a path variable, you need to provide a full path of java installed file to run every Java program on your system.

    >>C:\Program Files (x86)\Java\jdk1.7.0_67\bin\javac HelloWorld.java

    Is providing a complete path every time to run the Java program really convenient for you?

    If you set the path variable, you can run a Java program from any file directory without specifying the complete Java tool path.

    Yep! It is simple, convenient and easy to run any Java program by setting environment variables.

    What is the difference between Path and Classpath Variables:

    I have already covered this question in my previous article in detail. You can refer it to find the difference between Path and Classpath variable.

    How to check Java Path And Classpath Variables?

    You can rectify the path and classpath variable on the command prompt.

    Run the following commands. It will display all the directives in both path and classpath variables.

    This is the completed guide for JDK installation and setting Java variables. I tried to cover some frequently asked questions about the Java environment variables setup. If you have any more questions, feel free to write in the comment section below.

    Источник

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