1

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. Now what is happening is java gets installed and works fine only within the script. Once I come out of this script, none of the java functionalities work, not even 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_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
} 
Elina
  • 21
  • 1
  • After you exit the script, the JAVA_HOME and PATH are no longer in effect. Try setting them manually. Is there a reason why you are not using the (very commonly used) Java PPA (webupd8team)? – RealSkeptic Apr 19 '17 at 04:54
  • Define `none of the java functionalities work` - exes not found or something more sinister? – John3136 Apr 19 '17 at 04:56
  • @John3136 Functonality like `java-keytool` – Elina Apr 19 '17 at 05:02
  • @RealSkeptic I need everything to be done through script and nothing manual. – Elina Apr 19 '17 at 05:03
  • I suggest to check content of `$JAVA_BASENAME` because of this `JAVA_BASENAME=$(ls -1 $TMPDIR)` and `export JAVA_HOME=$TARGET/$JAVA_BASENAME`. – Cyrus Apr 19 '17 at 05:06
  • @Elina That is not an answer. What does it do? "Command not found" or it finds it then causes your computer to burst into flames or something else? – John3136 Apr 19 '17 at 05:09
  • @Cyrus both the values should be fine as JAVA is working fine within the script. I just need Java to work from everywhere, and not just within this script. – Elina Apr 19 '17 at 05:10
  • @RealSkeptic I already get a jdk bundled in a bin, and i need to use this same jdk.tgz file to install java. – Elina Apr 19 '17 at 05:12
  • @John3136 yes it gives command not found. And obviously does not burst into flames. Also, when i try `java -version`, it says java not installed. – Elina Apr 19 '17 at 05:14
  • This might help: [How to debug a bash script?](http://unix.stackexchange.com/q/155551/74329) – Cyrus Apr 19 '17 at 05:53
  • Then your scripts needs to update the system's profile so that those environment variables are available to all users. It will still require log out and log-in again. A script cannot set environment variables for its calling process or any other non-child process. – RealSkeptic Apr 19 '17 at 06:40

1 Answers1

0

Use update alternatives within your script to make your java installation available:

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. Alternatively you can write the export commands for JAVA HOME and PATH to your .bashrc from within your script (if using bash). This way the modified variables are available in the bash shell.

NaN
  • 7,441
  • 6
  • 32
  • 51