Set java verison to 8 permanently at terminal

Hi,
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 ?

Thanks
Rohit BajajAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
Java SE 10 has reached end of support. Users of Java SE 10 should switch to Java SE 11.
( https://www.oracle.com/technetwork/java/javase/downloads/index.html )
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

https://dzone.com/articles/installing-openjdk-11-on-macos is good for using aliases to switch between different versions
serialbandCommented:
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
...
INVOCATION
       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

CEHJCommented:
:)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.