Use openjdk as java

How To Install OpenJDK 18 On Ubuntu 20.04 LTS

It provides all the steps required to install OpenJDK 18 on Ubuntu 20.04 LTS and getting started with Java development using the Hello World example.

This tutorial provides all the steps required to install OpenJDK 18 on the popular Linux distribution i.e. Ubuntu. It provides all the steps for Ubuntu 20.04 LTS (Focal Fossa). The steps should be the same for other versions of Ubuntu, and Linux.

You can also follow the steps to install How To Install VSCode For Java On Ubuntu, How To Install IntelliJ IDEA for Java on Ubuntu, and How To Install Eclipse For Java Development On Ubuntu published by Tutorials24x7. You may also follow the How To Install OpenJDK 18 On Windows to install the latest OpenJDK on Windows and How To Install Java 18 On Windows to install Oracle JDK.

Download JDK

Open the JDK 18 GA Release site and download the distribution for Linux systems as highlighted in Fig 1.

Install JDK

Open the terminal and make the directory /usr/java/openjdk to keep all the java installations at the same place. We can install multiple versions of Java in this directory. Now copy the downloaded file to this location and extract it as shown below:

>sudo mkdir -p /usr/java/openjdk >cd /usr/java/openjdk >sudo cp /data/setups/openjdk-18.0.1.1_linux-x64_bin.tar.gz openjdk-18.0.1.1_linux-x64_bin.tar.gz >sudo tar -xzvf openjdk-18.0.1.1_linux-x64_bin.tar.gz

The above steps will install JDK to the path /usr/java/openjdk/jdk-18.0.1.1.

Set Environment Variables

In this step, we will configure the environment variable to use the JDK installed by us.

Scroll down by pressing Page Down Button and add at the end of this file:

# OpenJDK 18
JAVA_HOME=/usr/java/openjdk/jdk-18.0.1.1
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export PATH

Now press Ctrl + O and press Enter to write our change. Press Ctrl + X to exit the nano editor. The nano editor should be similar to Fig. 2.

Configure Java Commands

We can configure the Java commands to use the newly installed JDK by default. We can check the installed Java before and after executing these commands as shown below:

In case JDK is already installed on the system, it should show existing JDK as shown below.

# Check version
java -version

# Output
java version "18.0.1.1" 2022-04-22
Java(TM) SE Runtime Environment (build 18.0.1.1+2-6)
Java HotSpot(TM) 64-Bit Server VM (build 18.0.1.1+2-6, mixed mode, sharing)

It shows that Oracle JDK 18 was installed on my system. In case JDK is not installed on your system, the messages should be similar as shown below.

# Check version
java -version
# Output
Command 'java' not found, but can be installed with:

sudo apt install openjdk-11-jre-headless # version 11.0.15+10-0ubuntu0.20.04.1, or
sudo apt install default-jre # version 2:1.11-72
sudo apt install openjdk-16-jre-headless # version 16.0.1+9-1~20.04
sudo apt install openjdk-17-jre-headless # version 17.0.3+7-0ubuntu0.20.04.1
sudo apt install openjdk-8-jre-headless # version 8u312-b07-0ubuntu1~20.04
sudo apt install openjdk-13-jre-headless # version 13.0.7+5-0ubuntu1~20.04

If JDK is not installed on your system, use the below mentioned commands to configure the java commands.

# Configure Java Alternatives
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/java/openjdk/jdk-18.0.1.1/bin/java" 1
# Configure Javac Alternatives
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/java/openjdk/jdk-18.0.1.1/bin/javac" 1
# Check version
java -version

# Output
openjdk version "18.0.1.1" 2022-04-22
OpenJDK Runtime Environment (build 18.0.1.1+2-6)
OpenJDK 64-Bit Server VM (build 18.0.1.1+2-6, mixed mode, sharing)

In case JDK is already installed on your system, use the below mentioned commands to install JDK 18 with existing JDKs.

# Configure Java
sudo update-alternatives --config java

# Output
There are 2 choices for the alternative java (providing /usr/bin/java).

Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/java/oracle/jdk-18.0.1.1/bin/java 1 auto mode
1 /usr/java/openjdk/jdk-18.0.1.1/bin/java 1 manual mode
2 /usr/java/oracle/jdk-18.0.1.1/bin/java 1 manual mode

Press to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/java/openjdk/jdk-18.0.1.1/bin/java to provide /usr/bin/java (java) in manual mode

# Configure Java Compiler
sudo update-alternatives --config javac

# Output
There are 2 choices for the alternative javac (providing /usr/bin/javac).

Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/java/oracle/jdk-18.0.1.1/bin/javac 1 auto mode
1 /usr/java/openjdk/jdk-18.0.1.1/bin/javac 1 manual mode
2 /usr/java/oracle/jdk-18.0.1.1/bin/javac 1 manual mode

Press to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/java/openjdk/jdk-18.0.1.1/bin/javac to provide /usr/bin/javac (javac) in manual mode

The output of these commands is shown in Fig 3. We might need to configure active Java if it is previously installed on the system.

Читайте также:  What is php tidy

We can always check the active JDK as shown below.

# Check version
java -version

# Output
openjdk version "18.0.1.1" 2022-04-22
OpenJDK Runtime Environment (build 18.0.1.1+2-6)
OpenJDK 64-Bit Server VM (build 18.0.1.1+2-6, mixed mode, sharing)

These are the basic steps required to install the most recent version of OpenJDK on Ubuntu 20.04 LTS.

Hello World

In this section, we will write our first program in Java using the nano editor.

>sudo mkdir -p /data/programs/java
>cd /data/programs/java
>sudo nano HelloWorld.java

Now write the first program in Java as shown below, save the program and exit the editor.

// Hello World public class HelloWorld < // The main method public static void main( String[] args ) < // Print Hello World System.out.println( "Hello World !!" ); > >

Use the javac and java to compile and execute the program as shown below.

// Compile the program
sudo javac HelloWorld.java

// Execute the program
sudo java HelloWorld

// Program output
Hello Java !!

These are the basic steps to write, compile, and execute Java programs.

Summary

This tutorial provided all the steps to install OpenJDK 18 on Ubuntu 20.04 LTS and also provided the steps to configure it to use from the console. The last section explained the steps to write, compile, and execute the first Java program. You may submit your comments to join the discussion on installing OpenJDK 18 on Ubuntu and other Linux systems.

Источник

How to Install OpenJDK (Free Java) – Multi OS Guide

Otavio Ehrenberger

Otavio Ehrenberger

How to Install OpenJDK (Free Java) – Multi OS Guide

In a nutshell, there are two coexisting branches of Java: the proprietary, closed-source Oracle Java and the community-maintained open-source OpenJDK.

Читайте также:  Data analysis excel python

OpenJDK is licensed under GPL-2.0, and it consists of a Java Virtual Machine and a java-bytecode compiler. Since this is the easier and cheaper way, it’s the one we’re going to be using in this tutorial.

Here, you’ll learn how to install OpenJDK on Windows, Mac, and Linux in a few different ways.

How to Install OpenJDK

Very Easy Semi-Automatic Mode – for Windows and macOS

Keep in mind that this will require administrator access.

If you are in a hurry and just want a plug-and-play install with an easy uninstaller and automatic setup, that’s fine – I won’t judge. 🙂

Head over to the community-driven, Eclipse Foundation-supported Adopt Open JDK website to get the link for your installer (if you are in doubt, just go with OpenJDK 11 LTS on HotSpot JVM).

Also, Eclipse is the main open-source Java IDE in case you didn’t know.

You’ll be redirected to a page with a list of install links. Look for your OS, choose the packaged installer ( .msi for Windows or .pkg for macOS) and download it. Remember to install ALL features, as it won’t work out of the box if you don’t allow the installer to set JAVA_HOME. Then run it and voilà! You’re done.

Very Easy Semi-Automatic Mode – for Linux

This method also needs admin access, of course.

First, remember to run this command:

Your OS will very likely have its own OpenJDK package available in the repository manager.

For Ubuntu/Debian, the package names are usually named like openjdk—jre-headless . For example:

sudo apt install openjdk-8-jre-headless # installs for java 8
or
sudo apt install openjdk-13-jre-headless # installs for java 13

That’s it, the open-source community saves the day again.

Still Pretty Easy, Mostly Manual Mode – for Windows, macOS, and Linux

You can get your compressed OpenJDK from a number of different vendors such as Microsoft, Red Hat, Intel or anyone offering their fork of OpenJDK. They might even offer their own installer file. But to keep it simple we’re using Adopt Open JDK once again.

Select your preferred version and JVM ( OpenJDK 11 LTS on HotSpot JVM if you are unsure) and download the compressed JDK.

Why would you choose this option over the much easier methods just described above? Maybe you don’t have administrator rights on your current machine or maybe you are setting up your own strategy to manage multiple Java versions. I don’t know, but it has its use cases.

Steps for Windows

First, extract the zip file into a folder ( C:\Program Files\OpenJDK would be the educated choice. Note that \OpenJDK was manually added). It will create the folder for the JDK installation, with \bin as one of its sub-directories.

Читайте также:  Python return file object

You will need Administrator privileges to extract the zip file to this location.

If you cannot use Administrator rights for any reason, extract it to a location under your user space, such as C:\Users\%YOUR_USERNAME%\OpenJDK .

2. Open Environment Variables

Open the Control Panel > System & Security > System > Advanced System Settings (it’ll be under ‘Device Specifications in Windows 10+).

In the System Properties window, select the Advanced tab, then Environment Variables.

Under System Variables, click New. Enter the variable name as JAVA_HOME. Enter the variable value as the installation path of the JDK (appending the \bin sub-folder at the end of the path). Mine was C:\Program Files\OpenJDK\OpenJDK11U-jdk_x64_windows_hotspot_11.0.15_10\jdk-11.0.15+10\bin .

Click OK and Apply Changes. If you are doing this process as a non-admin, choose User Variables instead.

4. Add the binary executables to PATH:

Stay in the Environment Variables window. Click on the variable named Path (either for System or User, depending on your choice in the last section).

You’ll see a list of stuff. These are the executables you have access from your CLI (like Windows Terminal, Command Prompt, or Poweshell).

Click on ‘New’ at the top-right corner and add %JAVA_HOME% as a variable.

Click OK and Apply Changes.

Open a Command Line Interface. Type java -version . If the output was the version, all was OK, congrats!

If it wasn’t, restart your computer and try again. If it still isn’t working, double-check this tutorial, try to read your JAVA_HOME path, and see if it points to the the bin folder within the downloaded folder’s path.

Steps for Linux/macOS:

Then, extract the compressed file appropriate to your OS. In case you can’t or don’t want to use admin permissions, extract it somewhere in your user space (like ~/.openjdk ).

If you want a more conventional location, extract it to /usr/local/ , which is where software manually installed by the user conventionally goes in POSIX systems.

My command (for Linux) was this one: sudo tar -xf OpenJDK11U-jdk_x64_linux_hotspot_11.0.16.1_1.tar.gz -C /usr/local .

2. Set JAVA_HOME and add it to PATH:

Set JAVA_HOME to where you extracted your OpenJDK installation. Point it to the OpenJDK directory, not to its /bin subfolder, as JAVA_HOME will not only be used to determine the executables’ location.

This should be located in your shell initialization file. For example, let’s suppose These are the two last lines of my ~/.zshrc file:

 export JAVA_HOME="/usr/local/jdk-11.0.16.1+1" export PATH="$JAVA_HOME/bin:$PATH" 

Now, refresh your shell by either sourcing the init file or opening another tab/window.

You can check installation with java -version . If no errors are displayed, congratulations! Time to do some Java’ing.

Wrapping Up

And that’s it! Now you should have OpenJDK installed and ready to use on your machine. Thanks for reading.

Источник

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