How to find java on linux

Find the Java SDK location on Linux

Finding software’s installation directory is a very common operation. One common reason is for updating the PATH environment variable. For example, Java developers are often interested in finding the installation directory of Java. This article shows how to find the location of the JDK on Linux. The method described here works on both 32-bit and 64-bit versions of these operating systems.

The JDK includes many tools that can be used by programmers. In this article we will look at some of them. We will start with the most basic tool: the java command.

The Java Command

The java command is one of the most important commands available to Java programmers. It allows you to run programs written in the Java programming language. The java command has two main uses. First, it lets you execute Java code directly from the command line. Second, it provides an easy way to build your own Java applications.

To use the java command, type java followed by the name of the class or package containing the program you want to run. For example, if you have a file called HelloWorld.java in the current working directory, then you could run the following command to compile and run the program −

$ javac HelloWorld.java $ java HelloWorld

If you don’t specify any arguments after the java command, then the default JRE version is used. You can also set the JAVA_HOME environment variable to point to a different JRE.

Multiple Java in a System

You may need to install additional versions of the Java runtime environment (JRE) when using multiple versions of Java on your system. To do so, you must use the update−java−alternatives command. This command updates the symbolic links that link to the various versions of the JRE.

For example, suppose you have installed three versions of the JRE on your system: 1.5.0_10, 1.6.0_20, and 1.7.0_25. If you wanted to use the latest version of the JRE, you would first remove all existing versions of the JRE −

$ sudo apt-get purge openjdk*

Then you would create a new symbolic link pointing to the newest version of the JRE −

sudo ln -sf /usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java /usr/bin/java

Finally, you would update the symbolic links that point to other versions of the JRE to point to the newly created symbolic link− sudo update−alternatives − − config java

This process ensures that the correct version of the JRE is always selected when you invoke the java command.

Читайте также:  Python compile to executable linux

Java Development Kit Installation Directory

The JDK contains several components, including the compiler, development libraries, and tools needed to develop Java applications. These components are typically stored in subdirectories under the JDK installation directory. On Linux, the JDK installation directory is usually located at /usr/lib/java/.

Note that the JDK installation directory may vary depending on which distribution you are using. For example, on Ubuntu 10.04 LTS, the JDK is located at /usr/share/java/.

Finding the JDK Installation Directory

There are several ways to locate the JDK installation directory on Linux. The easiest way is to check the contents of the /etc/profile file. The /etc/profile file contains information about where the user’s home directories are located as well as the location of the JDK installation directory for each user account.

Using the update-java-alternatives Command

Update-Java-Alternatives updates all alternatives for the Java runtimes and development kits. We can use it together with the -l argument when searching for the JDK or JVM installation directory −

$ update-java-alternatives -l java-1.14.0-openjdk-amd64 1411 /usr/lib/jvm/java-1.14.0-openjdk-amd64

Using the update-alternatives Command

The update−alternative tool maintains symbolic links to determine which programs are used by the system when no specific program is specified. You can combine the −jar command line option with the −list option to list the locations of the Java SDK or JRE.

$ update-alternatives --list java /usr/lib/jvm/java-14-openjdk-amd64/bin/java

The which command displays the full path of a file and the readlink command solves the symbolic link. We can use a combination of these commands to find the location of the JDK and JRE respectively −

$ readlink -f $(which javac) /usr/lib/jvm/java-14-openjdk-amd64/bin/javac $ readlink -f $(which java) /usr/lib/jvm/java-14-openjdk-amd64/bin/java

If you use the −f option, then it will follow every symbolic link in recursive fashion.

Conclusion

We’ve covered several different methods for finding the location of Java. We can use these command line tools in our daily lives while using the Linux operating systems.

Источник

How to find my current JAVA_HOME in ubuntu?

To display JAVA_HOME variable path, type in terminal:

If nothing appears then set it with this:

export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64 

This will differ according to your JDK type and version.

For displaying it again, follow the first command.

Follow JREs from different vendors on the same system, for using different JDK’s or switch between JDK’s.

It gives «/usr/lib/jvm/java-6-sun». But I have installed java 7. When I check it using «java -version» it gives java version «1.7.0_45» Java(TM) SE Runtime Environment (build 1.7.0_45-b18) Java HotSpot(TM) Server VM (build 24.45-b08, mixed mode)

then execute second command for setting JAVA_HOME variable. NOTE: JAVA_HOME doesn’t make jdk default, it just makes JAVA_HOME variable set to a path & if you want to use different jdk installed on same machine then check my answer, I have edited it.

@Jax-L But now when I give echo JAVA_HOME it just displays as «JAVA_HOME». The path I gave is not displaying.

export works only until you restart. Or you add export to the .bashrc login script. But the correct way to set such environment variables is in /etc/environment

Читайте также:  Base64 to jpg python

If you have JDK 1.6 (corresponding to Java 6) or a newer version installed, you should have a program named jrunscript in your PATH . You can use this to find the corresponding JAVA_HOME . Example:

$ jrunscript -e 'java.lang.System.out.println(java.lang.System.getProperty("java.home"));' /opt/local/jdk1.7.0_76/jre 

You could set the environment variable like this:

$ export JAVA_HOME="$(jrunscript -e 'java.lang.System.out.println(java.lang.System.getProperty("java.home"));')" 

Note that the JRE doesn’t include jrunscript , so this will only work if you install the JDK, not just the JRE.

Источник

How to find path where jdk installed?

I’ve installed jdk1.7.0.rpm package in RHEL6.
Where I do find the path to execute my first java program?

Hi, Mohammad. It’s not a stupid question, but one that has been answered in several places already — like this or this question.

Just an PS: on MacOS, Java is usually installed at ‘/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home’, where the version number could be different.

4 Answers 4

For your first java program read this tutorial:

Note these commands give different results. If you are interested in the non-symlink path use whereis java.

I don’t really think this answers the question. The java binary gets installed with the JRE, but if you’re doing development you need JDK, which isn’t necessarily installed in which java (which in my case is /usr/bin).

On RHEL7, you can use locate :

and it led me to the /usr/lib/jvm/ directory which contained the directories:

java-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64/ jre/ jre-1.8.0/ jre-1.8.0-openjdk/ jre-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64/ jre-openjdk/ 

Each of these contain a bin/java

To find the full path of the symbolic link use:

This gave me mostly JDK6 even though java -version gave me 1.8. There was one link to the JDK8 folder near the top.

You can list the installed files with

You will see somewhere a bin directory with java executable

But if the JDK RPM was correctly installed you should already find java in you path.

javac MyFirstJavaClass.java 

and if everything compiles

(If you didn’t change anything the current directory . should already be in your class path)

Since this question is RPM specific, rpm is the way to get started (as answered by @Matteo).

-q is short for --query -l is short for --list 
rpm -ql jdk1.8.0_20 | grep "jdk1.8.0_20/bin$" 

Knowing this may be desirable for setting a user or application’s $JAVA_HOME variable. This is often needed when a system has multiple versions of java installed, or multiple distributions of java installed, such as OpenJDK and Oracle/Sun.

$JAVA_HOME Example

In the ~/.bash_profile , or related file ( .bashrc , .zshrc , .cshrc , setenv.sh ), something similar to the below may be used.

JAVA_HOME='/usr/java/jdk1.8.0_20' export JAVA_HOME PATH="$JAVA_HOME/bin:$PATH" export PATH 

If you would like more control over where Java gets installed, such as in /opt , then the tarball can be used instead of the RPM file.

Other similar questions, are asking about how to find any binary or file, in the general case.

Источник

How to Check Java Version Installed on Linux

How do I check my current Java version? There are several ways to check if Java is installed and which version is running on your system.

Читайте также:  Поиск элементов массива java

In this tutorial, learn how to check the Java version installed on Linux distros, including Ubuntu, CentOS, and Debian.

tutorial on checking Java version on linux

  • A user account with sudo privileges
  • Access to the command-line/terminal window
  • A version of Java

Method 1: Check the Java Version On Linux

To check the Java version on Linux Ubuntu/Debian/CentOS:

2. Run the following command:

3. The output should display the version of the Java package installed on your system. In the example below, OpenJDK version 11 is installed.

example of checking the version of java running on ubuntu linux

Note: If the output indicates there is no such package on the system, you can install it with the help of one of our guides – How to install Java on Ubuntu or How to Install Java on CentOS.

You can also check the version of the primary Java compiler – javac (pronounced “java-see”) with the command:

Check javac version on Ubuntu.

Method 2: Find Version by Checking Path Where Java is Installed

There are two ways to find the path of the Java directory.

The first option includes running a single command:

update-alternatives --list java

The system should respond with the path where Java is installed.

Check Java directory path to find the java installation version

Note: This option may not work on CentOS systems. If you have issues finding the path of the Java directory with the command above, use the alternative outlined below.

Alternatively, you can use the whereis command and follow the symbolic links to find the Java path.

Check Java path on Ubuntu.

The output tells you that Java is located in /usr/bin/java.

2. List the content of the /usr/bin/java directory:

Locate Java directory on Ubuntu.

Inspecting the directory shows that /usr/bin/java is only a symbolic link for /etc/alternatives/java.

3. Just like in the previous step, list the content of the provided path by running:

Find Java path on Ubuntu.

Finally, the output displays /etc/alternatives/java is another symbolic link and that the real path of the Java directory is /usr/lib/jvm/java-11-openjdk-amd64/bin/java.

Method 3: Search for Java in the Installed Packages List

You can also prompt the system to list installed packages and search for Java, with its version number.

Find Java by listing all installed packages.

1. To generate a list of all installed packages, use the command:

2. Scroll up/down until you find the Java packages as shown in this example.

Find Java in the installed packages list.

To avoid searching through all installed packages, list Java packages only. Prompt the system to list a specific software package. In this case, the package name is openjdk:

sudo apt list --installed | grep -i openjdk

Search for Java in the Installed packages list on Ubuntu.

Note: CentOS users need to modify the commands for listing installed packages for their package manager. Use the commands: sudo yum list installed and sudo yum list installed | grep -i openjdk instead.

With this article, you have successfully checked the Java version installed on Linux. We also covered checking the Java path and searching for Java among the installed packages.

Once the Java version is confirmed, you can start developing anything from lightweight mobile to desktop applications.

Источник

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