Score:1

How to export the correct java version into a bash script

ng flag

Ubuntu 18.04.6 LTS
Bash 4.4.0

I have 4 versions of java on my Linux box. I also have multiple MC Game Servers running on this box. When I run certain servers that require Java 8, I point to Java 8 like so:

#!/bin/bash

java="/usr/lib/jvm/java-8-openjdk-amd64/bin"  

When I run certain servers that require Java 16, I point to Java 16 like so:

#!/bin/bash

java="/usr/lib/jvm/java-16-oracle/bin"  

So in an attempt to properly import these versions of java into variables, I edited the /etc/profile file and everything from #== Java 7 and below is added by me:

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "${PS1-}" ]; then
  if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

#== Java 7
#== ======
JAVA7=/usr/lib/jvm/zulu7-ca-amd64
PATH=$PATH:$HOME/bin:$JAVA7/bin

#== Java 8
#== ======
JAVA8=/usr/lib/jvm/java-8-openjdk-amd64
PATH=$PATH:$HOME/bin:$JAVA8/bin

#== Java 11
JAVA11=/usr/lib/jvm/java-11-openjdk-amd64
PATH=$PATH:$HOME/bin:$JAVA11/bin

#== Java 16
JAVA16=/usr/lib/jvm/java-16-oracle
PATH=$PATH:$HOME/bin:$JAVA16/bin

export JAVA7
export PATH
export JAVA8
export PATH
export JAVA11
export PATH
export JAVA16
export PATH

If this is correct when I use JAVA16 in a script, how do I call for it in my start script?

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.