tutorial-cloud/blog/2019-05-29-blog-25.md
2024-10-25 10:05:08 +05:30

7.3 KiB

slug title authors tags
Getting Java Ready Installation on Ubuntu 12.04 Made Simple Getting Java Ready Installation on Ubuntu 12.04 Made Simple
slorber
yangshun
hola
docusaurus

import CodeBlock from '@site/src/components/CodeBloack';

Introduction
Before you dive into other cool things, you need to have Java on your computer. This guide will help you easily install and handle different versions of Java on Ubuntu 12.04.
Default JRE/JDK Installation Made Simple
This is the best and simplest choice. It will put OpenJDK 6 on Ubuntu 12.04 and older versions, and OpenJDK 7 on 12.10 and newer.

Installing Java with apt-get is straightforward. First, let's update the list of available software packages:

<CodeBlock code={ sudo apt-get update} />

Next, let's see if Java isn't already on your system:

<CodeBlock code={java -version} />

If you see a message saying "The program java can be found in the following packages," it means Java isn't installed yet. In that case, run this command:

<CodeBlock code={sudo apt-get install default-jre} />

This command installs the Java Runtime Environment (JRE). If you require the Java Development Kit (JDK), often needed for compiling Java applications like Apache Ant, Apache Maven, Eclipse, and IntelliJ IDEA, use the following command:

<CodeBlock code={sudo apt-get install default-jdk} />

Typically, you only need the JDK if you're going to build Java programs or if your software specifically asks for it, in addition to Java. The JDK includes the JRE, so there's no downside to installing it, except for the larger file size.
Remember, the remaining steps are optional and should only be done when necessary.

Advanced Installation: Including OpenJDK 7 (Optional)
To add OpenJDK 7 to your system, just run this command:

<CodeBlock code={sudo apt-get install openjdk-7-jre} />

If you want the Java Development Kit (JDK) instead of just the Java Runtime Environment (JRE), use this command:

<CodeBlock code={sudo apt-get install openjdk-7-jdk} />

For Advanced Users: Installing Oracle JDK (Optional Step)
The Oracle JDK is the main JDK, but it's not included by default in Ubuntu anymore.

You can still get it with apt-get. To install any version, just follow these steps:

<CodeBlock code={sudo apt-get install python-software-properties sudo add-apt-repository ppa:webupd8team/java sudo apt-get update } />

After that, depending on the version you need, run one of these commands:

Oracle JDK 6 Made Simple: Installation Steps
Even though it's an older version, some people still use it.

<CodeBlock code={sudo apt-get install oracle-java6-installer} />

Simple Setup: Installing Oracle JDK 8
This is a developer preview version, and the official release is coming soon. If you're curious about what Java 8 is all about, check out this article.

<CodeBlock code={sudo apt-get install oracle-java8-installer} />

Managing Java Versions (Optional Step)
If you have different versions of Java on your system, you can choose which one to use by running this command:

<CodeBlock code={sudo update-alternatives --config java} />

It might show something like this if you have 2 installations (or more, if you have more):

<CodeBlock code={`There are 2 choices for the alternative java (providing /usr/bin/java).

Selection Path Priority Status

  • 0 /usr/lib/jvm/java-7-oracle/jre/bin/java 1062 auto mode 1 /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java 1061 manual mode 2 /usr/lib/jvm/java-7-oracle/jre/bin/java 1062 manual mode Press enter to keep the current choice[*], or type selection number: `} />
Now, pick the number of the version you want as the default. You can also do this for the Java compiler (javac):

<CodeBlock code={sudo update-alternatives --config javac} />

It's the same screen as before, and you use it in the same way. You can use this command for other things in Java that have different versions, like keytool, javadoc, and jarsigner.

Setting up 'JAVA_HOME': How to Make Java Work Right
To set up the JAVA_HOME environment variable, which some programs need, start by finding where Java is installed on your computer:

<CodeBlock code={sudo update-alternatives --config java} />

It will show something like this:

<CodeBlock code={`There are 2 choices for the alternative java (providing /usr/bin/java).

Selection Path Priority Status

  • 0 /usr/lib/jvm/java-7-oracle/jre/bin/java 1062 auto mode 1 /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java 1061 manual mode 2 /usr/lib/jvm/java-7-oracle/jre/bin/java 1062 manual mode

Press enter to keep the current choice[*], or type selection number: `} />

The installation path is listed for each one:
  1. /usr/lib/jvm/java-7-oracle
  2. /usr/lib/jvm/java-6-openjdk-amd64
  3. /usr/lib/jvm/java-7-oracle
Copy the path from the Java installation you like the most. Then, open and edit the file called /etc/environment:

<CodeBlock code={sudo nano /etc/environment} />

In that file, put in the following line (but replace YOUR_PATH with the path you copied):

<CodeBlock code={JAVA_HOME="YOUR_PATH"} />

Now, to make sure everything's set up right, reload this file:

<CodeBlock code={source /etc/environment} />

Check if everything is working by running:

<CodeBlock code={echo $JAVA_HOME} />

If it shows the path you just set, then you've done it right! If not, double-check and make sure you followed all the steps correctly.

Conclusion:
Great job! Now you've got Java up and running on your Ubuntu 12.04 computer. Whether you picked the default OpenJDK or went for the Oracle JDK, you're all set. We even learned how to manage different Java versions and set up the 'JAVA_HOME' thing. That's a big win!

Ready for more coding adventures?