How to find java on linux

How to Install Java on Ubuntu 20.04 and Set Path[Simple Way]?

Java is a class based, object oriented programming language which is widely used to develop enterprise class applications.

In this tutorial, you’ll learn difference between JRE, Open JDK and Oracle JDK, install Java Runtime Environment on ubuntu and install java on Ubuntu 20.04 and set java_home path on Ubuntu 20.04 step by step.

Installing Java is to Install Java Development kit on Ubuntu.

Prerequisites

Before you start, you’ll need the following.

  • If you do not have servers in the cloud, Create an AWS EC2 Ubuntu server instance by following the guide How to launch an EC2 Instance.
  • [Important] – Update the packages list in the server which is upgrade-able using sudo apt update
  • [Important] – Upgrade the packages in the server to the latest versions using sudo apt upgrade

JRE vs Open JDK vs Oracle JDK

It is important to understand the difference between jdk vs OpenJDK and Oracle JDK before you install and use it.

JRE – It is a Java runtime environment which is used to run the Java based applications. It is sufficient to install Java Runtime Environment (JRE) if you just want to run the Java based programs and no need to use any Java developer Tools.

Open JDK – It is a free and opensource implementation of the Java SE Platform Edition developed by Oracle, OpenJDK and the Java community. JDK is a Java Development Kit which consists of all the necessary things to develop a Java program. If you need to A new version of OpenJDK is release every six months. Support is provided only for the latest version of the OpenJDK. It is free to use under GNU General Public License (GNU GPL) version 2.

Oracle JDK – It is a version fully developed by Oracle Corporation and it has some licensing implications. Public updates for Oracle Java SE 8 released after January 2019 will not be available for commercial, business, or production use without a commercial license, as announced by Oracle.

Now, you’ll learn how to install JRE, Open JDK and Oracle JDK in Ubuntu.

Installing JRE on Ubuntu (Default)

In this section, you’ll install default JRE on Ubuntu. Default JRE means the latest LTS version of the JRE which is currently Java 11.

JRE is already included in the JDK package. If you are going to install JDK version, then you can skip this section and directly install JDK using the next section.

Use the apt command line utility with the install command to install the default JRE.

sudo apt install default-jre

Installing Open JDK (default)

In this section, you’ll install the default Open JDK version.

Читайте также:  Linux cannot connect to socket

The default Open JDK version currently is Java 11. It is the latest LTS version of Java.

Use the apt command line utility with the install command to install the default OpenJDK.

sudo apt install default-jdk

Open JDK is installed. You can verify it using the java -version command.

You’ll see the output as below with the details of the Java versions installed.

openjdk version "11.0.9.1" 2020-11-04 OpenJDK Runtime Environment (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04) OpenJDK 64-Bit Server VM (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04, mixed mode, sharing) 

Default Open JDK is installed on Ubuntu. Now, you’ll install Open JDK8.

Installing Open JDK 8

In this section, you’ll install the Open JDK version 8.

Java 8 is most widely used Java version. Hence, if your applications require Java version 8 on Ubuntu, you can install Java 8 using the below command.

sudo apt install openjdk-8-jdk

If you use the Java -version command, you’ll still see the Java version 11 . Because you have installed OpenJDK 11 first. You’ll learn how to manage the default Java version on Ubuntu in the next step.

You’ve installed Open JDKs.

Setting the Default Java Version

In this section, you’ll set the default Java version. Because You’ve installed two versions of the JDK in the previous steps.

First to check the current default Java version, use the command Java -version.

You’ll see the current default version of Java as below.

openjdk version "11.0.9.1" 2020-11-04 OpenJDK Runtime Environment (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04) OpenJDK 64-Bit Server VM (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04, mixed mode, sharing) 

To change the default java version, update-alternatives utility with –config option.

Use the below command to update the default Java version.

sudo update-alternatives --config java

You’ll see the below prompt which will display the available versions of Java. The one marked with the * is the current default version.

Enter your desired version to set as the default Java version. In this example, you’ll select 2.

There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode 2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode Press to keep the current choice[*], or type selection number:2

You’ll see the below output which says Java 8 open JDK is set a default Java version.

update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java to provide /usr/bin/java (java) in manual mode

To validate if its set properly, use the below command.

You’ll see the below output.

openjdk version "1.8.0_275" OpenJDK Runtime Environment (build 1.8.0_275-8u275-b01-0ubuntu1~20.04-b01) OpenJDK 64-Bit Server VM (build 25.275-b01, mixed mode)

You’ve set the default Java version to Open JDK version “1.8.0_275” successfully. Next, you’ll set Java_home environment variable.

Setting Java_Home on Ubuntu

Applications developed using Java use the environment variable Java_home to determine the Java installation location. In this section, you’ll set Java_home environment variable. Environment variables are normally defined in the file /etc/environment .

Now, you’ll find the Java installation locations using the update-alternatives command as below.

sudo update-alternatives --config java

It’ll display the Java installation locations and it shows the default Java version too as below.

There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto m ode 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode * 2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode Press to keep the current choice[*], or type selection number: 

Press Enter to keep the current choice.

  • OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java
  • OpenJDK 8 is located at /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
Читайте также:  Получить все файлы директории linux

/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java is set as default Java as denoted with * . You can copy this path.

Now open the /etc/environment using the nano editor using the below command.

It’ll open the environment file. Append the java path configuration as shown below.

 PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin" JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java" 

Presss Ctrl + X and Press Y and then press Enter to save the file and exit.

You can logout and login again or run source command as below to make the environment path changes effective your current session.

You can validate if the Environment variable is correctly set by using the echo command. Echo command will display the value of the variable passed along with it.

You’ll see the below output.

/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

You’ve successfully set Java_home for Java on Ubuntu.

Uninstalling Java

If you want to uninstall Java for any reason, you can do it by using the apt remove command as shown below.

sudo apt remove openjdk-8-jdk

You can remove the specific java version by using that version name appropriately in the highlighted text.

Conclusion

In this tutorial, You’ve leanrt the difference between JRE, Open JDK and Oracle JDK. You’ve installed JRE, Open JDK 11 and Open JDK8, set the default Java version when you have multiple java versions installed. You’ve also set Java_home environment variable for Java.

You have learnt, How to install Java runtime environment on Ubuntu, how to install Java on Ubuntu, set java_home for Java and set default Java version on Ubuntu.

Источник

Where can I find the Java SDK in Linux after installing it?

I installed JDK using apt-get install but I don’t know where my jdk folder is. I need to set the path for that. Does any one have a clue on the location?

14 Answers 14

This depends a bit from your package system . if the java command works, you can type readlink -f $(which java) to find the location of the java command. On the OpenSUSE system I’m on now it returns /usr/lib64/jvm/java-1.6.0-openjdk-1.6.0/jre/bin/java (but this is not a system which uses apt-get ).

On Ubuntu, it looks like it is in /usr/lib/jvm/java-6-openjdk/ for OpenJDK, and in some other subdirectory of /usr/lib/jvm/ for Suns JDK (and other implementations as well, I think).

For any given package you can determine what files it installs and where it installs them by querying dpkg. For example for the package ‘openjdk-6-jdk’: dpkg -L openjdk-6-jdk

I think readlink as given is most elegant (and efficient), but I suggest readlink -f $(which javac) — note the ‘c’ in case there is a strange mix of JDK and JRE on the same machine. The JDK includes a compiler (javac) and a JRE does not. However if path is not correct, which will fail and you can try this: find /usr/java -wholename ‘*ava/jdk*’ -prune as I note in a comment below.

will tell you which java implementation is the default for your system and where in the filesystem it is installed. Check the manual for more options.

Читайте также:  Linux list package versions

@dequis, it’s an answer specific to debian, since the question mentioned apt-get and the distro to be Debian 2.6.26 . AFAIK, it should be the same for all debian derivatives.

should give you something like

This does not actually point to a full JDK. 1. It is a symlink, and even if you read the symlink, the binary is also not within a JDK. For example, if I run the command readlink -f $(which javac) it prints /usr/lib/jvm/java-8-oracle/bin/javac . That bin folder is NOT a JDK. General acid-base test to see if its a JDK is to see if the current $JAVA_HOME contains a path of lib/tools.jar . In the cast of /usr/lib/jvm/java-8-oracle/bin that is not true, therefore it is not a JDK.

On Centos / RHL This is what I prefer to find the JDK (if installed) find /usr/java -wholename ‘*ava/jdk*’ -prune But behavior depends whether you are talking about OpenJDK or Oracle Java and how it was installed in the first place.

This question will get moved but you can do the following

«find / -name ‘javac'» is less typing, but requires admin (root) privilege or you will get a lot permission denied messages.

Use find to located it. It should be under /usr somewhere:

When running the command, if there are too many «Permission denied» message obfuscating the actual found results then, simply redirect stderr to /dev/null

find /usr -name java 2> /dev/null 

Another best way to find Java folder path is to use alternatives command in Fedora Linux (I know its for Ubuntu but I hit this post from google just by its headline). Just want to share incase people like me looking for answers for fedora flavour.

To display all information regarding java

alternatives --display java 

Three Step Process: First: open Terminal-> $ whereis java it would give output like this: java: /usr/bin/java /usr/share/java /usr/share/man/man1/java.1.gz

Second: ls -l /usr/bin/java It would give output like this: lrwxrwxrwx 1 root root 22 Feb 9 10:59 /usr/bin/java -> /etc/alternatives/java

Third: ls -l /etc/alternatives/java output is the JDK path: lrwxrwxrwx 1 root root 46 Feb 9 10:59 /etc/alternatives/java -> /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

It’s /usr/local/java/jdk[version]

This question still seems relevant, and the answer seems to be a moving target.

On my debian system (buster):

> update-java-alternatives -l java-1.11.0-openjdk-amd64 1111 /usr/lib/jvm/java-1.11.0-openjdk-amd64 

However, if you actually go look there, you’ll see there are multiple directories and symbolic links placed there by the package system to simplify future maintenance.

The actual directory is java-11-openjdk-amd64 , with another symlink of default-java . There is also an openjdk-11 directory, but it appears to only contain a source.zip file.

Given this, for Debian ONLY, I would guess the best value to use is /usr/lib/jvm/default-java , as this should always be valid, even if you decide to install a totally different version of java, or even switch vendors.

The normal reason to want to know the path is because some application wants it, and you probably don’t want that app to break because you did an upgrade that changed version numbers.

Источник

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