Digao
Digao

Reputation: 560

JAVA_HOME variable gets reset

I've stumbled upon a maven problem which, according to stackoverflow community, should be a JAVA_HOME issue. So I went to ubuntu terminal and echoed JAVA_HOME. My terminal showed an empty line after the command:

rodrigo@rodrigo-DC1B-S:~$ echo $JAVA_HOME

rodrigo@rodrigo-DC1B-S:~$ 

So, after some research, I tried this: How to set Oracle's Java as the default Java in Ubuntu?

and this: Make $JAVA_HOME easily changable in Ubuntu

which lead me to try the export command. It works, but if I close the terminal and open again the problem comes back. I even changed the /etc/environment to set the JAVA_HOME in the file, like:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
JAVA_HOME=/usr/lib/jvm/java-8-oracle

Again, if a close the terminal and open again and echo $JAVA_HOME the result is the same, the empty line. What is the problema after all ? Why I can't have this variable set ? Oddly, if a echo the PATH variable it works:

rodrigo@rodrigo-DC1B-S:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

My java version is:

rodrigo@rodrigo-DC1B-S:~$ java -version
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)

Upvotes: 2

Views: 4109

Answers (3)

Muriithi Derrick
Muriithi Derrick

Reputation: 342

On MacOs Big Sur

  1. add this file ~/.zprofile if it does not exist touch ~/.zprofile
  2. add the desired paths, mycase is java and maven. run nano ~/.zprofile add this.
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home
export JAVA_HOME
export PATH=$JAVA_HOME/bin:$PATH

M2_HOME=/opt/apache-maven
export M2_HOME
export PATH=$M2_HOME/bin:$PATH
  1. exit nano and ..
  2. reload profile source ~/.zprofile

Upvotes: 0

jbrahy
jbrahy

Reputation: 4415

Sounds like you need to set that JAVA_HOME variable in ~/.profile (this is assuming you are using bash as your $SHELL)

vi  ~/.profile

add

export JAVA_HOME=/usr/lib/jvm/java-8-oracle

then exit the terminal and start a new one and you'll see that in your session.

env | grep JAVA_HOME                
JAVA_HOME=/usr/lib/jvm/java-8-oracle

Upvotes: 1

amanshri93
amanshri93

Reputation: 73

On Your .bash_profile write the following line:

export JAVA_HOME=/usr/lib/jvm/java-8-oracle

Upvotes: 1

Related Questions