Set java verison to 8 permanently at terminal

Rohit Bajaj
Rohit Bajaj used Ask the Experts™
I want to set the java version at terminal permanently to java 8.
Currently by default it is set to java 10.
I always have to do export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

How do i avoid this everytime ?

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fractional CTO
Distinguished Expert 2018
Add this to your bash startup script, for one or more users.

Normally this will be ~/.bashrc + keep in mind sourcing this file may be skipped for background (non-interactive) sessions, so be sure you set this variable above any checks for interactivity.

Note: Be sure to do this for the root user too, if you're running nightly CRON jobs under the root user.
Top Expert 2016
Java SE 10 has reached end of support. Users of Java SE 10 should switch to Java SE 11.
( )
Uninstalling Java 10 will cure the current problem. If you want to have a Java 11 environment around, just download it without installation. How you'd do that would depend on your environment.
Actually how you'd get your path right (per your original question) also really depends on your environment. David's solution, while generic and effective, is not really optimal for Debian (and possibly derivatives - i can't say exactly as i don't use derivatives). I suspect your environment is Mac, about which i know little is good for using aliases to switch between different versions
Normally this will be ~/.bashrc + keep in mind sourcing this file may be skipped for background (non-interactive) sessions

That's incorrect.
   .bashrc is for non-login shells.
   .bash_profile is for login shells.

The definitive information is in man bash.  Don't google, because you'll get stupid bloggers that don't know how to read the man pages and have incomplete or incorrect information.  Google needs to have a search that doesn't include blogs.

man bash
       A login shell is one whose first character of argument zero is a -, or one started with the --login option.

       An interactive shell is one started without non-option arguments and without the -c option whose standard  input
       and  error are both connected to terminals (as determined by isatty(3)), or one started with the -i option.  PS1
       is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.

       The following paragraphs describe how bash executes its startup files.  If any of the files exist but cannot  be
       read,  bash reports an error.  Tildes are expanded in file names as described below under Tilde Expansion in the
       EXPANSION section.

       When bash is invoked as an interactive login shell, or as a non-interactive shell with the  --login  option,  it
       first  reads and executes commands from the file /etc/profile, if that file exists.  After reading that file, it
       looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and  executes  commands  from
       the  first  one  that  exists  and is readable.  The --noprofile option may be used when the shell is started to
       inhibit this behavior.

       When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.

       When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc,
       if  that  file  exists.   This may be inhibited by using the --norc option.  The --rcfile file option will force
       bash to read and execute commands from file instead of ~/.bashrc.

Open in new window

Top Expert 2016


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial