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

150 lines
7.3 KiB
Markdown

---
slug: Getting Java Ready Installation on Ubuntu 12.04 Made Simple
title: Getting Java Ready Installation on Ubuntu 12.04 Made Simple
authors: [slorber, yangshun]
tags: [hola, docusaurus]
---
<!-- truncate -->
import CodeBlock from '@site/src/components/CodeBloack';
<div className="head">Introduction</div>
<div className="text">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.</div>
<div className="head">Default JRE/JDK Installation Made Simple</div>
<div className="text">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.</div><br/>
<div className="text">Installing Java with apt-get is straightforward. First, let's update the list of available software packages:</div>
<CodeBlock code={` sudo apt-get update`} /><br/>
<div className="text">Next, let's see if Java isn't already on your system:</div>
<CodeBlock code={`java -version`} /><br/>
<div className="text">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:</div>
<CodeBlock code={`sudo apt-get install default-jre`} /><br/>
<div className="text">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:</div>
<CodeBlock code={`sudo apt-get install default-jdk`} /><br/>
<div className="text">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.</div>
<div className="text">Remember, the remaining steps are optional and should only be done when necessary.</div><br/>
<div className="head">Advanced Installation: Including OpenJDK 7 (Optional)</div>
<div className="text">To add OpenJDK 7 to your system, just run this command:</div>
<CodeBlock code={`sudo apt-get install openjdk-7-jre`} /><br/>
<div className="text">If you want the Java Development Kit (JDK) instead of just the Java Runtime Environment (JRE), use this command:</div>
<CodeBlock code={`sudo apt-get install openjdk-7-jdk`} /><br/>
<div className="head">For Advanced Users: Installing Oracle JDK (Optional Step)</div>
<div className="text">The Oracle JDK is the main JDK, but it's not included by default in Ubuntu anymore.</div><br/>
<div className="text">You can still get it with apt-get. To install any version, just follow these steps:</div>
<CodeBlock code={`sudo apt-get install python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
`} /><br/>
<div className="text">After that, depending on the version you need, run one of these commands:</div><br/>
<div className="text" style={{ fontSize:'28px' }}>Oracle JDK 6 Made Simple: Installation Steps</div>
<div className="text">Even though it's an older version, some people still use it.</div>
<CodeBlock code={`sudo apt-get install oracle-java6-installer`} /><br/>
<div className="text" style={{ fontSize:'28px' }}>Simple Setup: Installing Oracle JDK 8</div>
<div className="text">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.</div><br/>
<CodeBlock code={`sudo apt-get install oracle-java8-installer`} /><br/>
<div className="head">Managing Java Versions (Optional Step)</div>
<div className="text">If you have different versions of Java on your system, you can choose which one to use by running this command:</div>
<CodeBlock code={`sudo update-alternatives --config java`} /><br/>
<div className="text">It might show something like this if you have 2 installations (or more, if you have more):</div>
<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:
`} /><br/>
<div className="text">Now, pick the number of the version you want as the default. You can also do this for the Java compiler (javac):</div>
<CodeBlock code={`sudo update-alternatives --config javac`} /><br/>
<div className="text">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.</div><br/>
<div className="head">Setting up 'JAVA_HOME': How to Make Java Work Right</div>
<div className="text">To set up the JAVA_HOME environment variable, which some programs need, start by finding where Java is installed on your computer:</div>
<CodeBlock code={`sudo update-alternatives --config java`} /><br/>
<div className="text">It will show something like this:</div>
<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:
`} /><br/>
<div className="text">The installation path is listed for each one:</div>
<div className="text"><ol><li>/usr/lib/jvm/java-7-oracle</li><li>/usr/lib/jvm/java-6-openjdk-amd64</li><li>/usr/lib/jvm/java-7-oracle</li></ol></div>
<div className="text">Copy the path from the Java installation you like the most. Then, open and edit the file called /etc/environment:</div>
<CodeBlock code={`sudo nano /etc/environment`} /><br/>
<div className="text">In that file, put in the following line (but replace YOUR_PATH with the path you copied):</div>
<CodeBlock code={`JAVA_HOME="YOUR_PATH"`} /><br/>
<div className="text">Now, to make sure everything's set up right, reload this file:</div>
<CodeBlock code={`source /etc/environment`} /><br/>
<div className="text">Check if everything is working by running:</div>
<CodeBlock code={`echo $JAVA_HOME`} /><br/>
<div className="text">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.</div><br/>
<div className="con">Conclusion:</div>
<div className="text">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! </div><br/>
<div className="text"><strong>Ready for more coding adventures?</strong></div>