Actions

Linux Java

From Ever changing code

Ubuntu: Oracle Java 8 JDK - Java Development Kit - apt-get

Silent install Java 6/7/8/9 using WebUpd8 team's PPA

#sudo apt-get install -y python-software-properties #if required
sudo apt-get install -y debconf-utils
sudo add-apt-repository -y ppa:webupd8team/java
sudo apt-get update
echo "oracle-java8-installer shared/accepted-oracle-license-v1-1 select true" | sudo debconf-set-selections
sudo apt-get install -y oracle-java8-installer

CentOS: Oracle Java 8u171 JDK - Java Development Kit - rpm

This methos does not require manually accepting the license

wget --header "Cookie: oraclelicense=accept-securebackup-cookie" [link from download page]
#jdk-8u171
wget --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u171-b11/512cd62ec5174c3487ac17c61aaa89e8/jdk-8u171-linux-x64.rpm
sudo yum -y localinstall jdk-8u171-linux-x64.rpm

Linux: Oracle Java JDK 9 from tar.gz

wget http://download.java.net/java/GA/jdk9/9/binaries/jdk-9+181_linux-x64_bin.tar.gz

Linux: Oracle Java JDK 10.0.1 from tar.gz

Go to Oracle and accept their license then you can download the file with cookie details provided.

#not verified if it's working
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://link_copied_from_site"
wget --no-check-certificate --no-cookies --header \
"Cookie: oraclelicense=accept-securebackup-cookie;" \
"http://download.oracle.com/otn-pub/java/jdk/10.0.1+10/fb4372174a714e6b8c52526dc134031e/serverjre-10.0.1_linux-x64_bin.tar.gz"
sudo tar -xzvf serverjre-10.0.1_linux-x64_bin.tar.gz -C /usr/local
sudo alternatives --install /usr/bin/java java /usr/local/jdk-10.0.1/bin/java 2
sudo alternatives --config java
sudo bash -c "echo export PATH=$PATH:/usr/local/jdk-10.0.1/bin >> /etc/environment" #PATH update
sudo bash -c "echo export JAVA_HOME=/usr/local/jdk-10.0.1 >> /etc/environment" #JAVA_HOME set up

Ubuntu: OpenJDK

You can use this Java open source installation when Oracle licensing causing limitations.

sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt-get install openjdk-8-jdk -y

Ubuntu: Oracle Java 7 JDK - zip file install

This will treat how to install Oracle Java in Ubuntu. This method does not use any package management software. Use a link in the references section for other options.

Download the 32-bit or 64-bit Linux from "http://www.oracle.com/technetwork/java/javase/downloads/index.html" "compressed binary file" - it has a ".tar.gz" file extension.

Uncompress it

tar -xvf jdk-7u80-linux-x64.tar.gz (32-bit)
tar -xvf jdk-7u80-linux-x64.tar.gz (64-bit)


The JDK 7 package is extracted into ./jdk1.7.0_80 directory.

Now move the JDK 7 directory to /usr/lib

sudo mkdir -p /usr/lib/jvm
sudo mv ./jdk1.7.0_80 /usr/lib/jvm/


Optional: Environment variables

$ sudo vi /etc/environment      #add environment variables system wise, all users
PATH="search/paths''':/usr/lib/jvm/jdk-oracle/bin'''"
JAVA_HOME=/usr/lib/jvm/jdk-oracle

Log log out and log back in, you should have Java available to you now.

Now run

sudo update-alternatives --install "/usr/bin/java"   "java"   "/usr/lib/jvm/jdk1.7.0_80/bin/java"   1
sudo update-alternatives --install "/usr/bin/javac"  "javac"  "/usr/lib/jvm/jdk1.7.0_80/bin/javac"  1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.7.0_80/bin/javaws" 1


This will assign Oracle JDK a priority of 1, which means that installing other JDKs will replace it as the default. Be sure to use a higher priority if you want Oracle JDK to remain the default.

Correct the file ownership and the permissions of the executables:

sudo chmod a+x /usr/bin/java
sudo chmod a+x /usr/bin/javac
sudo chmod a+x /usr/bin/javaws
sudo chown -R root:root /usr/lib/jvm/jdk1.7.0_80


Run

sudo update-alternatives --config java


You will see output similar to the one below - choose the number of jdk1.7.0_80 - for example 3 in this list (unless you have have never installed Java installed in your computer in which case a sentence saying "There is nothing to configure" will appear):

$ sudo update-alternatives --config java


There are 3 choices for the alternative java (providing /usr/bin/java).

Selection    Path                                  Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-6-oracle1/bin/java   1047      auto mode
  1            /usr/bin/gij-4.6                       1046      manual mode
  2            /usr/lib/jvm/java-6-oracle1/bin/java   1047      manual mode
  3            /usr/lib/jvm/jdk1.7.0_80/bin/java      1         manual mode

Press enter to keep the current choice [*], or type selection number: 3
 update-alternatives: using /usr/lib/jvm/jdk1.7.0_80/bin/java to provide /usr/bin/java (java) in manual mode


Repeat the above for:

sudo update-alternatives --config javac
sudo update-alternatives --config javaws


Verify

$ java -version
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)

References