简体   繁体   English

通过bash脚本在Ubuntu 16.04上安装Java7

[英]Install Java7 on Ubuntu 16.04 through bash script

I have below function java_install written in a bash script to install java on Linux box, to which I pass jdk-1.7.0_80-linux-x64.tgz as JAVA_PACKAGE . 我有一个用bash脚本编写的函数java_install来在Linux机器上安装java ,我将jdk-1.7.0_80-linux-x64.tgz作为JAVA_PACKAGE传递给它。 Now what is happening is java gets installed and works fine only within the script. 现在发生的事情是java安装并且只在脚本中正常工作。 Once I come out of this script, none of the java functionalities work, not even java -version . 一旦我从这个脚本中走出来,java功能都不起作用,甚至java -version没有。 Could someone please help me on what I might be missing here? 有人可以帮我解决一下我在这里可能缺少的东西吗? Basically, I just want java to be installed permanently on this box once this script is executed. 基本上,我只想在执行此脚本后将java永久安装在此框中。

java_install() {
local JAVA_PACKAGE=$1
local TMPDIR=/tmp/quickstart
local TARGET=/usr/share
    if [ -n "$JAVA_PACKAGE" ] && [ -f "$JAVA_PACKAGE" ]; then
  rm -rf $TMPDIR
  mkdir -p $TMPDIR
  cp $JAVA_PACKAGE $TMPDIR
  ( cd $TMPDIR && tar fxz $JAVA_PACKAGE && rm $JAVA_PACKAGE )
  local JAVA_BASENAME=$(ls -1 $TMPDIR)
  mkdir -p $TARGET
  if [ -d "$TARGET/$JAVA_BASENAME" ]; then
    echo "# Java already installed at $TARGET/$JAVA_BASENAME"
    log_info "Java already installed at $TARGET/$JAVA_BASENAME"
  else
    echo "# Java now installed at $TARGET/$JAVA_BASENAME"
    log_info "Java now installed at $TARGET/$JAVA_BASENAME"
    mv $TMPDIR/$JAVA_BASENAME $TARGET
  fi
  rm -rf $TMPDIR

  # now create a script to export these settings
  export JAVA_HOME=$TARGET/$JAVA_BASENAME
  export PATH=$JAVA_HOME/bin:$PATH
else
  echo "# cannot find java package to install"
  log_error "cannot find java package to install"
fi
} 

Use update alternatives within your script to make your java installation available: 使用脚本中的update alternatives使您的Java安装可用:

sudo update-alternatives --install "/usr/bin/java" "java" "path to you java executable" 1

More information on this topic can be found here: How to use the command update-alternatives --config java . 有关此主题的更多信息,请参见此处: 如何使用命令update-alternatives --config java Alternatively you can write the export commands for JAVA HOME and PATH to your .bashrc from within your script (if using bash). 或者,您可以从脚本中将JAVA HOMEPATH的导出命令写入.bashrc (如果使用bash)。 This way the modified variables are available in the bash shell. 这样,修改后的变量在bash shell中可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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