简体   繁体   中英

Installing Java 1.8 on Ubuntu Linux from Binary

Oracle hosts a page on how to install Java 1.8 from the binary

I was able to do the following -

Download the x64 tar.gz on the Oracle Downloads page.

Create my directory and move the file there

sudo mkdir /usr/local/java
sudo mv ~/Downloads/jdk-8u45-linux-x64.tar.gz .

And unpack it

sudo tar zxvf jdk-8u45-linux-x64.tar.gz

The binary itself works, because I can call it using the absolute path

> /usr/local/java/jdk1.8.0_45/bin/java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

But I can't call it standalone

> java -version
The program 'java' can be found in the following packages:
 * default-jre
 * gcj-4.6-jre-headless
 * gcj-4.7-jre-headless
 * openjdk-7-jre-headless
 * openjdk-6-jre-headless
Try: sudo apt-get install <selected package>

Even setting $JAVA_HOME as that directory didn't work. Is there a step I'm missing? Does it need to be added to my $PATH ?

Edit: I'm aware I could probably install java with sudo apt-get install . I ran into some trouble there in that I wasn't able to apt-get update because of some 404 errors. I'd rather handle that as a separate question/post.

Thanks!

You have just extracted the binary file but you didn't set JAVA_HOME . First set the JAVA_HOME in your profile ie in ~/.bashrc file.

export JAVA_HOME=/usr/local/java/jdk1.8.0_45
export PATH=$PATH:$JAVA_HOME/bin

Reload the ~/.bashrc file as

> source ~/.bashrc press enter

Then try again.

> java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

The best way to do this is to use update alternatives not using JAVA_HOME. I downloaded the latest tarball jdk-8u162-linux-x64.tar.gz into a subdirectory ./src . I've also set up /opt so that it is owned by me, otherwise you need to use sudo for the untarring of the .tar.gz file.

#!/usr/bin/env bash

MAJOR_VERSION=8
ORACLE_BUILD_VERSION=162

JDK_TARBALL_VERSION=8u${ORACLE_BUILD_VERSION}-linux-x64
JDK_VERSION=1.${MAJOR_VERSION}.0_${ORACLE_BUILD_VERSION}
PRIORITY="10${MAJOR_VERSION}1"

cd src && tar xzf jdk-${JDK_TARBALL_VERSION}.tar.gz -C /opt/jdk

sudo update-alternatives --install /usr/bin/java java /opt/jdk/jdk${JDK_VERSION}/bin/java ${PRIORITY}
sudo update-alternatives --install /usr/bin/javac javac /opt/jdk/jdk${JDK_VERSION}/bin/javac ${PRIORITY}

echo "Checking: alternatives on java and javac"
sudo update-alternatives --display java
sudo update-alternatives --display javac

echo "Running: java -version"
java -version

I've included build 162 since this is much more recent that 45 in your message. This script above should be provided to everyone in the field of development since we can see currently that oracle-java8-installer for Debian and Ubuntu is broken as of yesterday. It might be fixed in a few days but this script will always work.

The priority value in the script is really important since you must override existing priority of the installed package. I've set it to use the major version to be installed. This priority would only fail if you have openjdk version 9 installed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM