Перейти к содержимому

Как проверить java home

  • автор:

Как найти JAVA_HOME

В этом кратком руководстве мы узнаем, как найти JAVA_HOME в Windows, Mac и Linux.

Как мы все знаем, JAVA_HOME — это переменная среды, которую мы обычно используем для поиска исполняемых файлов Java, таких как java и javac .

2. Способы поиска JAVA_HOME для Windows «​

Если мы используем Windows в качестве операционной системы, сначала нам нужно открыть нашу командную строку ( cmd ) и ввести:

Если JAVA_HOME определен в нашей среде, то приведенная выше команда распечатает его.

Или, чтобы показать местоположение исполняемого файла Java , мы можем попробовать:

3. Специфичные для macOS и Linux способы поиска JAVA_HOME ​

Если мы используем macOS или Linux, мы можем открыть наш терминал и ввести:

Если JAVA_HOME определен в нашей среде, то приведенная выше команда распечатает его.

Или мы могли бы попробовать:

Это, вероятно, просто показывает нам /usr/bin/java, что на самом деле не очень полезно, поскольку это символическая ссылка. Чтобы разгадать это, мы будем использовать dirname и readlink.

How to find where is $JAVA_HOME set? [duplicate]

I’m trying to change the value of $JAVA_HOME & I just can’t seem to find in which file is it being set currently. I can’t remember where did I set it the last time. Already tried How to determine where an environment variable came from? but I need(ed) a list of files where the variable can be set.

2 Answers 2

You didn’t specify a shell. So, I will assume bash . The next issue is: did you set it for your user only or system-wide? If you set it for your user only, then run:

If you set it system-wide, then it may vary with distribution but try:

If the above give no answer, you can cast a wider net:

John1024's user avatar

/.bashsrc but the changes didn't show up since I used different terminal instances to set & test the value of variables. Thanks for your help.

That will simulate a login shell and show everything that is done (except in areas where stderr is redirected with zsh ) along with the name of the file currently being interpreted.

So all you need to do is look for JAVA_HOME in that output. (you can use the script command to help you store that output).

Stéphane Chazelas's user avatar

    The Overflow Blog
Linked
Related
Hot Network Questions

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.8.2.43556

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

JAVA_HOME directory in Linux

Is there any linux command I could use to find out JAVA_HOME directory? I’ve tried print out the environment variables («env») but I can’t find the directory.

11 Answers 11

On Linux you can run $(dirname $(dirname $(readlink -f $(which javac))))

On Mac you can run $(dirname $(readlink $(which javac)))/java_home

I’m not sure about windows but I imagine where javac would get you pretty close

Just another solution, this one’s cross platform (uses java ), and points you to the location of the jre.

Outputs all of java ‘s current settings, and finds the one called java.home .

For windows, you can go with findstr instead of grep.

Parth Mehrotra's user avatar

echo $JAVA_HOME will print the value if it’s set. However, if you didn’t set it manually in your startup scripts, it probably isn’t set.

How to find your Java Home when running multiple versions

Coffee beans

When you install Java, it’s part of the installer’s job to tell your system where to find the right language when it’s needed. That’s why you’re able to run Java applications—in fact, you may sometimes run Java applications without ever realizing what language they’ve been written in.

Put simply, it just works, just as when you install Python to run a Python application, C to run an application written in C, and so on.

But what happens when you install more than one version of Java on your system? In that case, you’ll have to take on the extra responsibility of understanding where each version’s JAVA_HOME is located.

The easiest way to solve this issue is to use SDKMan. However, if you don’t have SDKMan available, you need to do it manually.

Why you might want multiple versions of Java

There are a few reasons you might want more than one version of Java on your computer.

Programming languages progress at one rate, while an application might develop at a different rate. It’s possible for Java (the language) to increment its version number while your favorite application sticks with an older version. If you have two must-have applications, each of which uses a different version of Java, you may want to install both.

Developers work on many different projects, some of which might use an old long-term support (LTS) version of Java while another uses the latest version.

You might be on a system for which you don’t have permission to install Java at the systemwide level. You can, instead, run Java out of your home directory, but that sometimes means you have to point applications to that nonstandard install location.

The JAVA_HOME variable

It’s perfectly acceptable to have more than one version of Java installed, but applications may not know where to find the version it needs.

Normally, an application just uses whatever you have set as your default Java runtime. This is something you may not have set yourself. Depending on what Linux distribution you run and what method you’ve used to install Java, your system default could be the first version of Java you installed or the most recent one.

Regardless of what your default is, if you try to launch an application and then receive an error about the version of Java you’re using, even though you know you’ve got the right version installed somewhere on your system, you can manually override the setting.

Applications can derive the location of Java from your current session’s .

First, take a look at what, if anything, JAVA_HOME is set to currently:

If there’s output, then your computer is maintaining a JAVA_HOME variable. If there’s no output, then your computer doesn’t have that variable set. That doesn’t necessarily mean your computer has no Java home, it just means that nothing’s bothered to set it yet.

Find the right version

Before you can set JAVA_HOME , you need to know which Java you want to use. Which Java you need to use depends on the application you’re trying to run. If an application requires Java 8 to run, you need to point it to the bin directory containing Java 8. If an application requires Java 17 to run, you need to point it to the bin directory containing Java 17.

You can find your current default Java version with the -version option. Launch a terminal and type this command (omit the $ command prompt):

Find the right location from a package manager

Now you need to know where the version of Java you want to use is located.

If you installed Java through a package manager, you can query your package manager for a file list. This tells you what and where files were installed with that package.

For example, on Fedora, CentOS, or Mageia, you can list all installed packages and to see just packages containing the string java :

For even cleaner output, use :

Assume you need information on Java 8 (version 1.8 ).
You can see each file in the Java 8 packages using the rpm command:

You may get more than one result, but look at the file paths carefully. Generally, you just want the Java executable in the primary bin directory of its Java distribution.

In this example, the path to Java 8 is:

If you use a different package manager, the commands are different, but the goal remains the same. Consult your package manager’s documentation for details.

Find the right location from a manual install

If you’ve manually installed Java from a .tar archive («tarball»), your package manager has no record of it. Hopefully, howerer, you know where you put it, so finding the path to its JAVA_HOME is as simple as listing the absolute path to its bin directory.

Here’s how I confirm that the directory I think ought to exist does in fact exist:

Set JAVA_HOME temporarily

Now you know the location of your desired JAVA_HOME .

To set a JAVA_HOME variable temporarily, you can precede the command you want to run with your variable setting. For instance, suppose you’re on a system with both Java 8 and Java 11 installed on it. Java 8 was installed originally, but you downloaded Java 11 because a cool new application you want to try requires it.

Evironment variables placed before the command you’re running get inherited by the command:

Set JAVA_HOME permanently with the alternatives command

To set a new default Java version permanently, use the alternatives command. The alternatives command looks at applications installed on your Linux system and lets you choose which version to use. Some distributions, such as Slackware, don’t provide an alternatives command, so you must use the local install method instead.

On Fedora, CentOS, Mageia, and similar distributions, the command is alternatives .

On Debian and Ubuntu and similar, the command is update-alternatives .

To get a list of available versions of an application currently installed on your Fedora system:

On Debian, you must specify the application you want alternatives for:

To choose which version you want to make the system default on Fedora and similar systems:

On Debian-based systems:

You can change the default Java version as needed, based on which application you want to run.

Choose your Java

Usually, you don’t have to worry about applications getting confused when using Java, but it’s a benefit of Java that it allows you to have different versions installed at once. Thanks to that feature, you can run even old software that hasn’t been updated to the latest version of Java, while still running the latest Java apps.

However, with that flexibility comes the responsibility of understanding how to guide applications to the correct version. Now you know how.

If this method seems like a lot of work, consider using SDKMan instead.

Learning and studying technology is the key to success

Manage Java versions with SDKMan

The SDKMan project makes it easy to manage different versions of Java and related languages, including Groovy, Scala, Kotlin, and more.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *