Set environment variables java home to your java environment

How to set JAVA_HOME environment variable on Windows 10

In this article, you will understand the details about JAVA_HOME environment variable and how to set it on Windows 10.

What is JAVA_HOME?

By convention, JAVA_HOME is the name of an environment variable on the operating system that points to the installation directory of JDK (Java Development Kit) or JRE (Java Runtime Environment) – thus the name Java Home. For example:

JAVA_HOME = c:\Program Files\Java\jdk1.8.0_201

Why is JAVA_HOME needed?

To develop Java applications, you need to update the PATH environment variable of the operating system so development tools like Eclipse, NetBeans, Tomcat… can be executed because these programs need JDK/JRE to function. So the PATH environment variable should include JAVA_HOME :

PATH = Other Paths + JAVA_HOME

Other paths are set by various programs installed in the operating system. If the PATH environment variable doesn’t contain a path to JRE/JDK, a Java-based program might not be able to run. For example, typing java in the command prompt showing this error:

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

error java command

Or Eclipse will fail to launch:

Eclipse launch error no Java home found

How to set JAVA_HOME on Windows 10

Here are the visual steps to properly set value for the JAVA_HOME and update the PATH environment variables in order to setup Java development environment on your computer:

1. Firstly, you need to identify the Java home directory, which is typically under C:\Program Files\Java directory. Open My Computer and navigate to this directory, you will see:

Java home directory

Here, the home of JDK is under C:\Program Files\Java\jdk1.80_201 . The version number may vary, depending on the JDK you installed.

2. Open the System Environment Variables dialog by typing environment in the search area on Start menu. Click the suggested item Edit the system environment variables:

start menu open system environment variable dialog

The System Properties dialog appears, click the button Environment Variables.

System properties dialog

Then you will see this dialog:

Environment Variables dialog

3.Create the JAVA_HOME environment variable by clicking the New button at the bottom. In the New System Variable form, enter the name and value as follows:

New JAVA HOME system variable

Click OK, and you will see the JAVA_HOME variable is added to the list.

4.Update the PATH system variable. In the Environment Variables dialog, select the Path variable and click Edit:

Edit Path variable

Then in the Edit environment variable dialog, double click on the empty row just below the last text line, and enter %JAVA_HOME%\bin as follows:

Edit Path variable for Java home

The percent signs tell Windows that it refers to a variable – JAVA_HOME , and the \bin specifies the location of java.exe and javac.exe programs which are used to run and compile Java programs, as well as other tools in the JDK.

Читайте также:  Html cache steam можно удалить

Click OK button to close all the dialogs, and you’re all set. Now you can open Eclipse or NetBeans to verify. Or open a command prompt and type in javac –version , you should see:

verify java home

You can add the path to the bin directory of Java home directly into the PATH variable. But it’s strongly recommend to follow the above steps to setup a JAVA_HOME variable because many Java programs depend on it.

When you installed a JDK, you might not need to update the system variable because the installer already did it for you.

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.

Источник

Переменная окружения JAVA_HOME

Во многих статьях в интернете, документации к инструментам для разработки на Java и в книгах зачастую упоминается JAVA_HOME. Что же такое JAVA_HOME?

Что такое JAVA_HOME

JAVA_HOME это переменная окружения, указывающая на директорию с установленным JDK (Java Development Kit, комплект разработчика Java). JAVA_HOME это соглашение, используемое во многих программах из экосистемы Java.

Какие программы используют JAVA_HOME

  • Intellij IDEA, Eclipse, NetBeans
  • Apache Maven, Apache Ant, Gradle
  • Apache Tomcat
  • Jenkins

Некоторые игры, написанные на Java (например, Minecraft), тоже могут требовать установленной переменной JAVA_HOME.

Ошибки, связанные с JAVA_HOME

Если переменная окружения JAVA_HOME не определена, некоторые программы могут выдавать следующие ошибки:

  • Переменная среды java_home не определена
  • Cannot determine a valid Java Home
  • JAVA_HOME is set to an invalid directory
  • JAVA_HOME is not defined correctly
  • JAVA_HOME environment variable is not set
  • JAVA_HOME command not found
  • JAVA_HOME not found in your environment
  • JAVA_HOME does not point to the JDK

При появлении таких ошибок просто установите переменную JAVA_HOME

Как установить переменную окружения JAVA_HOME в Windows

Сперва вам нужно установить JDK или JRE.

  • Установите JDK, если вы занимаетесь разработкой программ на Java
  • Установите JRE, если вам нужно только запустить прикладную программу на Java

После установки JDK либо JRE запишите путь установки, он понадобится.

Теперь щёлкните правой кнопкой на «Мой компьютер» → «Свойства» → «Дополнительные параметры системы» → «Переменные среды…». В разделе «Системные переменные» нажмите кнопку «Создать…» и укажите следующие данные:

Имя переменной JAVA_HOME
Значение переменной Путь к директории JDK / JRE, например:
C:\Java\jdk-11.0.6

Сохраните изменения, кликнув «OK». Теперь выберите в списке переменную окружения Path и нажмите «Изменить…». В конце списка добавьте строчку со значением «%JAVA_HOME%\bin«

Для проверки откройте консоль (Win+R, cmd) и укажите последовательно укажите две команды:

Если вы правильно установили JDK/JRE и правильно установили переменные окружения, вы увидите вывод наподобие этого:

Это будет служить результатом того, что переменная JAVA_HOME установлена правильно и Java работает из командной строки.

Резюме

В данной статье мы рассказали вам, что являет собой переменная окружения JAVA_HOME, где она используется и как её корректно указать.

Читайте также:  Массив вывод элементов php

Источник

How to set JAVA_HOME for Java?

You can set your JAVA_HOME in /etc/profile as Petronilla Escarabajo suggests. But the preferred location for JAVA_HOME or any system variable is /etc/environment .

Open /etc/environment in any text editor like nano or gedit and add the following line:

JAVA_HOME="/usr/lib/jvm/open-jdk" 

(java path could be different)

Use source to load the variables, by running this command:

Then check the variable, by running this command:

Usually most linux systems source /etc/environment by default. If your system doesn’t do that add the following line to ~/.bashrc (Thanks @pje)

When i tried to run Android Studio (that has IntelliJ IDEA as a base), i had an error message very similar to @advocate’s: «‘tools.jar’ seems to be not in Android Studio classpath.» After fiddling a lot with JAVA_HOME without success, i decided to take a look at studio.sh, the shellscript that starts Android Studio. As a wild guess, i set JDK_HOME to the same value expected for JAVA_HOME, and voila! It installed without great problems.

For those doing software development, don’t put your JAVA_HOME in /etc/environment unless you want to reboot everytime you switch JDK versions.

This is a temporary solution, as others pointed out. No one would want to run source every time they restart their bash.

As others have pointed out, this doesn’t stick between terminal sessions. What I did to address this is just added the line source /etc/environment to the top of my bash config file ~/.bashrc so that it loads all my environment settings on startup. Working for me so far.

Источник

How to set JAVA_HOME environment in Linux, Unix and Windows? Example

JAVA_HOME is a system environment variable that represents the JDK installation directory. When you install JDK in your machine (Windows, Linux, or UNIX) it creates a home directory and puts all its binary (bin), library(lib), and othe r tools. In order to compile the java program «javac» tool should be in your PATH and in order to get that in PATH we use the JAVA_HOME environment variable. Many tools like ANT and web servers like tomcat use JAVA_HOME to find java binaries. In this article, we will see how to set the JAVA_HOME environment variable in the different operating systems including Windows (Windows 7, Vista, XP) and Linux (Unix).

Setting JAVA_HOME on Windows environment:

How to set JAVA_HOME environment in Linux, Unix and Windows

Windows environments (Windows 7, Vista, XP) are GUI-based operating systems and you can edit the PATH system variable from GUI. you need to define another variable called JAVA_HOME and set the Java installation directory as a value for the JAVA_HOME variable.

An important thing to note here is that on the Windows environment (Windows 7, Vista, XP), you have two sections user-defined variables and System Variables. a variable declared in one section is not visible in other sections.

So if you set JAVA_HOME on the user variable section and update PATH on System variable section, you will not be able to get the value of JAVA_HOME and it will be blank. So be careful with this.

Читайте также:  Json decode example in php

And, If you are a Java beginner then I also recommend you go through these Java Beginner courses from Coursera and Udemy to learn Java in a better and more structured way. This is one of the best and up-to-date courses to learn Java online.

Setting JAVA_HOME on Unix or Linux

Setting JAVA_HOME on Unix or Linux environment is more easy and straightforward. you just need to export the JAVA_HOME variable and its value and then you can refer to its value in any shell script. here is an example of how to set JAVA_HOME on Unix and Linux:

Now Java is added to your system PATH by using the JAVA_HOME System variable. you can also check How to set PATH for Java in Linux or Unix for more details on steps and explanations.

JAVA_HOME vs java.home

I have seen Java programmer confusing over JAVA_HOME and java.home, where former represent JDK installation directory later represent JRE installation directory and you know Difference between JDK and JRE . JRE just contains java binaries and doesn’t contain other tools which come with JDK and are required for Java development. java.home is also a Java system property and you can access it on java code using System.getProperty(«java.home» ).

Also, all java system properties are exposed as ANT builtin property you can access java.home inside ANT as $

That’s all on how to set JAVA_HOME in Windows and Linux. We have also seen differences between JAVA_HOME and java.home system property. Let us know if you come across any other differences between them.

5 comments :

To set/export PATH for java-
export PATH= $:$/bin

One of the main advantage of using JAVA_HOME variable to point Java installation directory is that :

1) It’s easy to upgrade JDK without affecting your application startup and config file which points to JAVA_HOME. you just need to download new version and make sure your JAVA_HOME points to new version of Java. This is best benefit of using environment variable or links.

2) JAVA_HOME variable is short and concise instead of full path to JDK installation directory.

3) JAVA_HOME variable is platform independence i.e. if your startup script uses JAVA_HOME then it can run on Windows and UNIX without any modification, you just need to set JAVA_HOME on respective operating system.

4) JAVA_HOME is standard, which means other tools which needs Java e.g. Maven, Eclipse can refer this variable without having any knowledge of where Java is physically installed in your machine, which is obviously be different in different user’s machine. JAVA_HOME allows everybody to access Java in a common and standard way.

Let me know if this make sense.

Does Apache Maven also uses $JAVA_HOME to find out Java version to build a project? I am also confused with PATH and JAVA_HOME environment variable, if both contains location of JDK binary, which will take preference?

does setting JAVA_HOME variable is mandatory to run Java programs?

Источник

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