How to add JAVA_HOME and M2_HOME environment variables in Ubuntu

311 views Asked by At

Firstly, I am not very familiar with Ubuntu OS. I am a windows guy.

For a client requirement, I created a ubuntu virtual machine (22.04) in azure cloud and I had developed a bash script which installs the following:

  1. Java
  2. Maven
  3. Helm
  4. git, etc

I am using https://snapcraft.io/store to install the above mentioned tools.

The Problem:

I am unable to find JAVA_HOME and M2_HOME in the environment variables. It appears like snap is not setting these variables after installing Java & Maven.

I tried to do an export and set these variables using the "export" command but no luck. I do not know why but the variables are not present in my VM. I am not good at bash scripting, so I am stuck with this problem since weeks.

I need help in setting the JAVA_HOME and M2_HOME variables through script without any manual intervention after their installation.

Any help would be awesome! Thanks in advance :)

Here is my bash script:

#!/bin/bash

OC_VERSION="4.5.0-0.okd-2020-07-14-153706-ga"
OC_TAR_NAME="openshift-client-linux-4.5.0-0.okd-2020-07-14-153706-ga.tar.gz"

echo "Installing Tools..."
sudo apt-get update -y
sudo apt-get remove openjdk-11-jre-headless -y

sudo apt-get update -y

echo "Installing java..."
sudo snap install openjdk
export JAVA_HOME=/var/snap/openjdk/current/openjdk.env
export PATH=$JAVA_HOME/bin:$PATH
# source /var/snap/openjdk/current/openjdk.env

echo "Installing maven..."
sudo apt-get install maven -y

echo "Installing helm..."
sudo snap install helm --classic

echo "Installing kubelogin..."
sudo snap install kubelogin

echo "Installing kubectl..."
sudo snap install kubectl --classic

echo "Installing powershell..."
sudo snap install powershell --classic

echo -e "Installing git..."
sudo snap install git-ubuntu --classic

echo -e "Installing postgresql..."
sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install postgresql

echo -e "Downloading openshift-client..."
sudo wget https://github.com/openshift/okd/releases/download/4.5.0-0.okd-2020-07-14-153706-ga/${OC_TAR_NAME}

echo -e "Extracting openshift-client..."
sudo tar -zxvf ${OC_TAR_NAME} -C /usr/local/bin

# Remove downloaded tar files
sudo rm -f ${OC_TAR_NAME}

# Add environment variable settings to ~/.bashrc
echo 'export JAVA_HOME=/usr/lib/jvm/default-java' >> ~/.bashrc
echo 'export M2_HOME=/usr/bin/maven' >> ~/.bashrc
source ~/.bashrc
3

There are 3 answers

1
kev000 On

Try adding them to the /etc/environment or any other file other than .bashrc this is a weird situation, give this a try and here is a reference

5
Peng On

Summary

you define the export script in bashsrc which would load when the terminal is active , the cause of error not clearly about how you call the java --version command,but The more gereral usage is wired the variables into /etc/profile .

Interactive

  1. vim the /etc/profile
sudo vim /etc/profile
  1. copy your variables to profile example as below , instead of your correct path
export JAVA_HOME=/var/snap/openjdk/java-8u131
export PATH=$JAVA_HOME/bin:$PATH
  1. source profile
sudo source /etc/profile
  1. try the command
java --version

Non-Interactive

just use below command replace the step 2 , and other is in same

echo "export JAVA_HOME=/var/snap/openjdk/java-8u131" > /etc/profile
echo "export PATH=$JAVA_HOME/bin:$PATH" > /etc/profile

then the golbal variables has been defined , you can call the command anywhere

0
Kalyana Srinivas On

@Peng's answer was of great help to me and this blog post too.

I modified my export commands like the below and a reboot was needed for all of this to work.

echo "export JAVA_HOME=/snap/openjdk/1539/jdk" | sudo tee -a /etc/profile
echo "export PATH=$JAVA_HOME/bin:$PATH" | sudo tee -a /etc/profile
echo "export M2_HOME=/usr/bin/mvn" | sudo tee -a /etc/profile
echo "export JAVA_HOME" | sudo tee -a /etc/profile
echo "export M2_HOME" | sudo tee -a /etc/profile
echo "export PATH" | sudo tee -a /etc/profile

My Entire Bash Script:

#!/bin/bash

OC_VERSION="4.5.0-0.okd-2020-07-14-153706-ga"
OC_TAR_NAME="openshift-client-linux-4.5.0-0.okd-2020-07-14-153706-ga.tar.gz"

echo "Installing Tools..."
sudo apt-get update -y

echo "Installing java..."
sudo snap install openjdk

echo "Installing maven..."
sudo apt-get install maven -y

echo "Installing helm..."
sudo snap install helm --classic

echo "Installing kubelogin..."
sudo snap install kubelogin

echo "Installing kubectl..."
sudo snap install kubectl --classic

echo "Installing powershell..."
sudo snap install powershell --classic

echo -e "Installing git..."
sudo snap install git-ubuntu --classic

echo -e "Installing postgresql..."
sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install postgresql

echo -e "Downloading openshift-client..."
sudo wget https://github.com/openshift/okd/releases/download/4.5.0-0.okd-2020-07-14-153706-ga/${OC_TAR_NAME}

echo -e "Extracting openshift-client..."
sudo tar -zxvf ${OC_TAR_NAME} -C /usr/local/bin

# Remove downloaded tar files
sudo rm -f ${OC_TAR_NAME}

echo "export JAVA_HOME=/snap/openjdk/1539/jdk" | sudo tee -a /etc/profile
echo "export PATH=$JAVA_HOME/bin:$PATH" | sudo tee -a /etc/profile
echo "export M2_HOME=/usr/bin/mvn" | sudo tee -a /etc/profile
echo "export JAVA_HOME" | sudo tee -a /etc/profile
echo "export M2_HOME" | sudo tee -a /etc/profile
echo "export PATH" | sudo tee -a /etc/profile

terminal_output

I appreciate the contributions of everyone who shared their ideas to assist me in resolving this.