8

Using UBUNTU, I installed java 8 with the following command

$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer

$ sudo apt-get install oracle-java8-set-default

and I'm trying to set the java environment path as follow

$ cat >> /etc/environment <<EOL
JAVA_HOME=/usr/lib/jvm/java-8-oracle
JRE_HOME=/usr/lib/jvm/java-8-oracle/jre 
EOL

but I get this error message:

bash: /etc/environment: permission denied
Davide Buoso
  • 140
  • 1
  • 1
  • 5

5 Answers5

13
  1. Assuming that you have already the following:

     $sudo add-apt-repository ppa:webupd8team/java
     $ sudo apt-get update
     $ sudo apt-get install oracle-java8-installer
     $ sudo apt-get install oracle-java8-set-default*
    
  2. Open /etc/environment file with the following command.

    sudo nano /etc/environment
    

    N/B: You can replace nano with any other editor you like e.g atom

  3. At the end of file, add

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

The command above alone worked for me but you can also add the command below if you want.

JRE_HOME="/usr/lib/jvm/java-8-oracle/jre"

Remember the path used here was my java installation directory, if yours is the same then you don't need to change anything, otherwise use your path.

  1. Check whether your changes persisted

    $ source /etc/environment

    $ echo $JAVA_HOME

    https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-get-on-ubuntu-16-04

crakama
  • 759
  • 11
  • 25
2

Try this script, save it in a file.sh

#!/bin/bash
sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install openjdk-8-jre -y
sudo cat >> /etc/environment <<EOL
JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
JRE_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
EOL
SHR
  • 7,940
  • 9
  • 38
  • 57
thecloudguy
  • 331
  • 3
  • 8
1

Try with sudo

$ sudo cat >> /etc/environment <<EOL
JAVA_HOME=/usr/lib/jvm/java-8-oracle
JRE_HOME=/usr/lib/jvm/java-8-oracle/jre 
EOL
1

Use the following lines to set the path variables in the /etc/environment

echo export JAVA_HOME=/path/to/java | sudo tee -a /etc/environment
echo export JRE_HOME=/path/to/jre  | sudo tee -a /etc/environment

It should work.

Note:

You should reboot the system for changes to take effect.

And don't forget to come here after reboot and vote +1 :)-|-<

Jafar Amini
  • 315
  • 2
  • 8
  • https://superuser.com/questions/339617/how-to-reload-etc-environment-without-rebooting –  Oct 15 '21 at 08:21
1

Following is the method that worked for me -

Use bashrc to create environment variable :

  1. JAVA_HOME=/path/to/java
  2. JRE_HOME=/path/to/jre
  3. sudo vim ~/.bashrc
  4. Enter Password
  5. Press I(Key) to go in the insert mode
  6. Write export JAVA_HOME='/path/to/java' at the end of file
  7. Write export JRE_HOME='/path/to/jre' at the end of file
  8. Save and Exit (:wq)