This article assumes that you have some kind of Java software development environment installed on your system, such as Oracle Java, OpenJDK, or IBM Java. Otherwise, read or type in the terminal: sudo apt-get install openjdk-7-jdk
If Java is installed on your system, then your next task is to create a clear environment in order to create the first Java program. When creating programs, some users prefer to use the IDE (integrated development environment), for example, or. Such an environment makes programming easier when working with a large number of Java class files.
In this article, the IDE is not used, but the Java JDK (Java development kit - Java development kit) is used, a directory and a Java text file are created (using a text editor).
Install Java JRE / JDK on Linux
Today we suggest that you familiarize yourself with the different options for installing Java libraries, since all of them will be most useful and applicable in certain situations. For example, if you do not want to use third-party repositories or if you want to put several Java nearby, then you need to use a separate option. However, let's take a closer look at all of them.
First, it is recommended that you check for system storage updates and find out the current version of Java, if any, in the OS. All this is done through the standard console:
- Open the menu and run "Terminal".
Enter the command sudo apt-get update.
Enter the password from your account to gain root access.
If you receive a notification similar to the one below, it means that Java is not available in your OS.
What is the difference between JDK and JRE
- Jre - Java Runtime Environment is a Java runtime environment. Designed for normal use. Allows you to run applications written in the Java language.
- Jdk- Java Development Kit - A standard version of the Java platform designed for development. This is a special developer package, which includes documentation, various utilities, a compiler, class libraries, as well as the JRE itself.
DIY Java installation on Linux
You can download the archive with the version you need from the official site. Next, you need to transfer it to the desired directory and unzip it. I recommend / opt / java, then it will be used. Do this using the following commands:
sudo tar -xzf /opt/java/jre*.tar.gz
Where "*" - Java version.
Download the archive with development tools from the official website of Oracle. Also transfer it to the desired directory and unzip:
sudo tar -xzf /opt/java/jdk*.tar.gz
Install Java on Ubuntu
To install proprietary Oracle Java, you need to add a repository, update package indexes, and install Java. In Ubuntu, starting with the 18th release, this is done automatically after adding the repository.
Attention! Both JRE and JDK will be installed. First, add the repository and update the list of packages:
sudo add-apt-repository ppa: webupd8team / java
sudo apt-get update
During the installation process, you will need to accept the license agreement:
sudo apt-get install oracle-java8-installer
Install Java in ArchLinux
The official repositories have only OpenJDK. Therefore, you will have to use the package from AUR (you can, of course, install Java from Oracle with your own hands, but AUR is easier). Installation is done with just one command:
JDK can also be installed using AUR. Just run the command:
Install Java on CentOS
For RHEL, Fedora, Cent OS, OpenSUSE there is an official RPM package that you can download from the official site. To install the package from the terminal, use this command:
sudo rpm -i /path/to/file/jre-*.rpm
Where "*" - Java version. Or use any graphical utility.
You can download developer tools on the Oracle website, the link to which is at the top of the article. To install from the terminal, use the command:
Where "*" is the version of Java. "
"- indicates that the path begins.
Java version selection
To switch to version 8 of Java, use the following command:
sudo update-java-alternatives -s java-8-oracle
To automatically set environment variables, use the command:
sudo apt-get install oracle-java8-set-default
Or you can install the alternatives yourself. This is done using the following commands:
sudo update-alternatives --install / usr / bin / java java / usr / lib / jvm / java-8-oracle / bin / java 1
sudo update-alternatives --install / usr / bin / javaс javaс / usr / lib / jvm / java-8-oracle / bin / javaс 1
sudo update-alternatives --install / usr / bin / javaws javaws / usr / lib / jvm / java-8-oracle / bin / javaws 1
Now it remains to choose the versions of the java, javaws and javac executables that will be used by default:
sudo update-alternatives --config java
sudo update-alternatives --config javac
sudo update-alternatives --config javaws
To get started, view a list of installed environments:
Then install what you need:
archlinux-java set environment_name
archlinux-java set java-8-jre / jre
You can select the required environment using the commands given below (the same as in Ubuntu). You just need to select the number corresponding to the version number:
sudo update-alternatives --config java
sudo update-alternatives --config javac
sudo update-alternatives --config javaws
Setting environment variables
To make the settings accessible to all users, we will use the file / etc / profile. Open it for editing with any text editor using the command sudo and add the following lines to the end:
- export JAVA_HOME = / opt / java / jdk * /
- export JRE_HOME = / opt / java / jdk * / jre
- export PATH = $ PATH: / opt / java / jdk * / bin: / opt / java / jdk * / jre / bin
- Instead of the default path / opt / java, specify the one you used,
- If you use not JDK, but JRE, then along the way you will also have not "jdk *", but "jre *",
- "*" is the version of Java that you have installed.
In this article, we have examined in detail the process of installing and configuring Java in various Linux distributions. If you have any questions, ask in the comments!
No related posts
4 comments
A wonderful article is short and clear.
A small addition:
In addition to update-alternatives --config java, update-alternatives --config javac and update-alternatives --config javaws, at one time, when installing from of. binaries, I had a lot more to update-alternatives'it and --config'ur, otherwise annoying errors rained down. but this is the case if several versions of Java are installed. I will not describe here - too much text, see my instruction: http://w3bsit3-dns.com.ru/forum/index.php?showtopic=209610&st=9320#Spoil-44184239-15
Thank you for the article!
Please explain why you need to register environment variables?
If the system has only one user (admin), is there any point in this action?
It makes sense. The number of users has nothing to do with it. How are you going to tell the programs where your Java machine is located?
Method 1: Official Repositories
The easiest method is to use the official repository to download Java, which the developers uploaded there. You only need to register a few commands to add all the necessary components.
- Run "Terminal" and write sudo apt-get install default-jdk there, and then click on Enter.
Now add the JRE by typing the command sudo apt-get install default-jre.
The browser plug-in that is added via sudo apt-get install icedtea-plugin will also work.
If you are interested in obtaining documentation regarding added components, download them with the sudo apt-get install default-jdk-doc command.
Although this method is quite simple, it is not suitable for installing the latest Java libraries, since they have not been laid out in the official repository recently. That is why we suggest that you familiarize yourself with the following installation options.
Method 2: Webupd8 Repository
There is a user repository called Webupd8, which has a script comparing the current version of Java with the one on the Oracle site. This installation method is useful for those who want to install a newer release 8 (the last available in the Oracle repository).
- In the console, type sudo add-apt-repository ppa: webupd8team / java.
Be sure to include your password.
Confirm the add operation by clicking on Enter.
Expect file downloads to complete without closing "Terminal".
Update system storage with the sudo apt-get update command.
Now you should add the graphical installer by typing sudo apt-get install oracle-java8-installer.
Accept the license agreement to configure the package.
Agree to add new files to the system.
At the end of the process, a command will be available for you to install absolutely any version - sudo apt-get install oracle-java7-installer, where java7 - Java version. For example, you can write java9 or java11.
The sudo apt-get remove oracle-java8-installer command will help get rid of unnecessary installers, where java8 - Java version.
Method 3: Upgrade Using Webupd8
Above, we talked about installing assemblies by using the Webupd8 custom repository. Thanks to the same repository, you can update the Java version to the latest one just by means of a comparison script.
- Repeat the first five steps from the previous instructions if you have not already done these steps.
- Type in the command sudo update-java and then click on Enter.
Use the sudo apt-get install update-java command to install updates if they are found.
Method 4: Manual Installation
Perhaps this method is the hardest of those that we examined in this article, but it will allow you to get the necessary version of Java without using third-party repositories and other additional components. To accomplish this task, you will need any available browser and "Terminal".
- Via a web browser, go to the official Oracle page to download Java, where click on "Download" or select any other necessary version.
Below are several packages with libraries. We recommend downloading the format archive tar.gz.
Go to the archive folder, right click on it and select "Properties".
Remember the location of the package, because you have to go to it through the console.
Run "Terminal" and run the command cd / home / user / folder, where user - username, and folder - name of the archive storage folder.
Create a folder to unzip the archive. Usually all components are placed in jvm. Creating a directory is done by typing sudo mkdir -p / usr / lib / jvm.
Unpack the existing sudo tar -xf jdk-11.0.2_linux-x64_bin.tar.gz -C / usr / lib / jvm archive into the created folder jdk-11.0.2_linux-x64_bin.tar.gz - name of the archive.
To add system paths, you will need to enter the following commands sequentially:
sudo update-alternatives --install / usr / bin / java java /usr/lib/jvm/jdk1.8.0/bin/java 1
sudo update-alternatives --install / usr / bin / javac javac /usr/lib/jvm/jdk1.8.0/bin/javac 1
sudo update-alternatives --install / usr / bin / javaws javaws /usr/lib/jvm/jdk1.8.0/bin/javaws 1
One alternative path may not exist, depending on the version of Java you choose. It remains only to carry out the configuration of each path. First, run sudo update-alternatives --config java, find the appropriate version of Java, check its number and write to the console.
Repeat the same step with sudo update-alternatives --config javac.
Then configure the last path through sudo update-alternatives --config javaws.
As you can see, there are a large number of methods for installing Java in the Linux operating system, so each user will find a suitable option. If you use a specific distribution kit and the above methods do not work, carefully study the errors displayed in the console and use official sources to solve the problem.
Thank the author, share the article on social networks.