java JDK setup for linux(Ubuntu)

Java is popular for long time, and it become more welcomed for Google's Android, for developers.

But to enjoy it, you have to have an JDK(or JRE) at lest firstly.

even there is hundreds of articles introducing how to setup JAVA SDK, i still just add this duplicated SOPs, for Ubuntu.

only simple way provided to make it not too hard to follow.

at the end, i also provide another way that makes your special JDK only works for one terminal without changing the whole system wide JDK config.

<A>simple way to install JDK

the simplest way to install all needed Java developer utils by Ubuntu's famous apt-get:

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java6-installer
# or if you want JDK 7:
sudo apt-get install oracle-java7-installer
# or if you want JDK 8:
sudo apt-get install oracle-java8-installer
This installs JDK 7 (which includes Java JDK, JRE and the Java browser plugin), but you should remind that by using  the Web Up8 Oracle Java OOS, this PPA is sometimes out of date.  Andalso note the dangers of using a PPA. different Ubuntu version and JDK version(5/6/7/8) may also impact the exact cmd to install them.

And all after thses steps you can also enter the following command to check the result of the installation:

java -version
With java8 you should then get a terminal output like:

java version "1.8.0-ea"
Java(TM) SE Runtime Environment (build 1.8.0-ea-b54)
Java HotSpot(TM) Server VM (build 24.0-b21, mixed mode)

<B>install your JDK manually

At the sametime, some people may more likely to DIY from a downloaded JDK and config all manually to make sure all details under control.

here is how we can reach it.

  • Download the 32-bit or 64-bit Linux "compressed binary file" - it has a ".tar.gz" file extension.

  • Uncompress it

    tar -xvf jdk-8-linux-i586.tar.gz (32-bit)

    tar -xvf jdk-8-linux-x64.tar.gz (64-bit)

    The JDK 8 package is extracted into ./jdk1.8.0 directory. N.B.: Check carefully this folder name since Oracle seem to change this occasionally with each update.

  • Now move the JDK 8 directory to /usr/lib

    sudo mkdir -p /usr/lib/jvm
    sudo mv ./jdk1.8.0 /usr/lib/jvm/
    
  • Now run

    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
    

    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.8.0
    

    N.B.: Remember - Java JDK has many more executables that you can similarly install as above. javajavacjavaws are probably the most frequently required. This answer lists the other executables available.

<C>handle multi-JDK

if after installing your JDK, there comes out to be more than one JDK/JRE(version or brach) like:

sudo update-alternatives --config java
*0  /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   auto mode
1  /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   manual mode
2  /usr/lib/jvm/jdk1.7.0/bin/java                   manual mode
3  /usr/lib/jvm/jre1.7.0/bin/java                   manual mode
you can select the option number you'd like to make it as default.

repeat below two cmds as above too:

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

then your multi-JDK env all OK now.

you can also try below cmds directly(if java7 prefered):

sudo update-java-alternatives -s java-7-oracle
and cmds below to setup env variables

sudo apt-get install oracle-java7-set-default
it is said that "update-java-alternatives" will update all jre/jdk/jvm(like java/javc/javaws). compared with "update-alternatives --config java"..
and you can use "sudo update-alternatives --all" if you'd like to get the same advantages.
so some one even makes the all simple way to an script like:

if [[ $UID != 0 ]]; then
  echo "This script neeeds to be run with sudo, like this:"
  echo -e "\n  sudo $0 $*\n"
  exit 1
fi
apt-get install python-software-properties
add-apt-repository ppa:webupd8team/java
apt-get update
apt-get install oracle-java8-installer
if [ ! -f $HOME/Downloads/jdk-8u5-linux-*.tar.gz ]; then
  echo "The JDK tar.gz file was not found in $HOME/Downloads. please download it, put it in $HOME/Downloads and run again."
  exit 1
fi
cp ~/Downloads/jdk-8u5-linux-*.tar.gz /var/cache/oracle-jdk8-installer/
apt-get install oracle-java8-installer
apt-get install oracle-java8-set-default
java -version
remember to add "sudo" to run the script!

<D>another way

what if you want to keep the OS wide JDK config but only use on special version for current terminal only?

for example, you have many projects will be compiled by java, most projects can be comipled and ran on almost any JAVA env, but some projects will have to be compiled by different special JDKs.

here is what i do.

no matter if there is JDK installed or what the installed JDK version.

just download the JDK package you'd like, and uncompress it under special folder, under current user for example(it is best not put here)

ls ~/ | grep jdk
jdk1.5.0_22_amdx64
jdk1.6.0_26
jdk1.7.0_71
here we have JDK5/6/7 downloaded, and add java env variables in the end of    /etc/profile

#JDK 1.6
export JAVA_HOME=/home/flxue/jdk1.6.0_26
export JRE_HOME=/home/flxue/jdk1.6.0_26/jre
export CLASS_PATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASS_PATH
export PATH=.:$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
#JDK 1.5 x86_64
#export JAVA_HOME=/home/flxue/jdk1.5.0_22_amdx64
#export JRE_HOME=/home/flxue/jdk1.5.0_22_amdx64/jre
#export CLASS_PATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASS_PATH
#export PATH=.:$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
add JAVA_HOME/JRE_HOME/CLASS_PATH/PATH for all your JDKs and mark all items except the one you'd like to use as default for the whole system.
save and make it works.
for current config, JDK6 is the system wide default version,

to make one special terminal be different, you'd just type below cmd to run the script as below:

source exchange_jdk.sh 5
the param is just the JDK version, you can define and change the script to make it whatever you'd like.

to recover to the original JDK for current terminal, just call it with param 0.

and the script is

#!/bin/bash
#usage:
#exchange_jdk <jdk_version num>
#  JDK version number:
#    JDK1.5: 5;
#    JDK1.6: 6;
#    JDK1.7: 7;
#    use 0 to restore to the original JDK version

#no args: number of args <=0
JDK_VERSION=6
JDK_EXCHANGE=0
if [ $# -lt 1 ]; then
  echo "no args for jdk version, do  nothing"
  exit 1;
fi

#JDK 1.5
#JAVA_HOME_5=/home/flyuelee/jdk1.5.0_22_amdx64
#JRE_HOME_5=/home/flyuelee/jdk1.5.0_22_amdx64/jre
JAVA_HOME_5="$HOME/jdk1.5.0_22_amdx64"
JRE_HOME_5="$HOME/jdk1.5.0_22_amdx64/jre"
#JDK 1.6
JAVA_HOME_6="$HOME/jdk1.6.0_26"
JRE_HOME_6="$HOME/jdk1.6.0_26/jre"
#JDK 1.7
JAVA_HOME_7="$HOME/jdk1.7.0_71"
JRE_HOME_7="$HOME/jdk1.7.0_71/jre"
case $1 in
  5)
    export OLD_JAVA_HOME=$JAVA_HOME
    export OLD_JRE_HOME=$JRE_HOME
    export JAVA_HOME=$JAVA_HOME_5
    export JRE_HOME=$JRE_HOME_5
    JDK_EXCHANGE=1
  ;;
  6)
    export OLD_JAVA_HOME=$JAVA_HOME
    export OLD_JRE_HOME=$JRE_HOME
    export JAVA_HOME=$JAVA_HOME_6
    export JRE_HOME=$JRE_HOME_6
    JDK_EXCHANGE=1
  ;;
  7)
    export OLD_JAVA_HOME=$JAVA_HOME
    export OLD_JRE_HOME=$JRE_HOME
    export JAVA_HOME=$JAVA_HOME_7
    export JRE_HOME=$JRE_HOME_7
    JDK_EXCHANGE=1
  ;;
  0)
    export JAVA_HOME=$OLD_JAVA_HOME
    export JRE_HOME=$OLD_JRE_HOME
    JDK_EXCHANGE=1
  ;;
esac

#needs to modify current JDK
if [ $JDK_EXCHANGE -ne 0 ]; then
  echo "exchange jdk from $OLD_JAVA_HOME to $JAVA_HOME"
  export CLASS_PATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASS_PATH
  export OLD_PATH=$PATH

  #remove all old/duplicate JDK env
  #PATH=${PATH##JAVA_HOME_5}
  i=1
  TMP_PATH="."
  while((1==1))
  do
    tmp=`echo $PATH | cut -d ':' -f$i`
    if [ "$tmp" != "" ]; then
      ((i++))
      if [[ $tmp == $JAVA_HOME_5* ]] || [[ $tmp == $JRE_HOME_5* ]]; then
        echo "ignore $tmp"
      elif [[ $tmp == $JAVA_HOME_6* ]] || [[ $tmp == $JRE_HOME_6* ]]; then
        echo "ignore $tmp"
      elif [[ $tmp == $JAVA_HOME_7* ]] || [[ $tmp == $JRE_HOME_7* ]]; then
        echo "ignore $tmp"
      elif [ "$tmp" == "." ]; then
        echo "ignore $tmp"
      else
        TMP_PATH=$TMP_PATH:$tmp
      fi
    else
      break;
    fi
  done
  TMP_PATH=${TMP_PATH##".:"}
  echo "after remove all--> $TMP_PATH"
  export PATH=.:$JAVA_HOME/bin:$JRE_HOME/bin:$TMP_PATH
fi
echo "current jdk--> $JAVA_HOME"
echo "old     jdk--> $OLD_JAVA_HOME"
#build code
#######
echo "current PATH--> $PATH"

to make the script seen any where by current user,

you can put it into your /home/<usr>/bin and export this folder into your PATH env variable.

i have to say, in fact the provided links clarify more details and should have more info you'll be interested in.

REF

http://askubuntu.com/questions/56104/how-can-i-install-sun-oracles-proprietary-java-jdk-6-7-8-or-jre

http://stackoverflow.com/questions/12787757/how-to-use-the-command-update-alternatives-config-java

http://askubuntu.com/questions/315646/update-java-alternatives-vs-update-alternatives-config-java

http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值