1

I have two shell scripts.

$ cat java_alternative_7
#!/bin/sh
sudo update-java-alternatives -s java-7-oracle

$ cat java_home_7
#!/bin/sh
export JAVA_HOME=/usr/lib/jvm/java-7-oracle

It's simple that I can execute/source these two files.

$ sh java_alternative_7
$ javac -version
javac 1.7.x_yy

$ source java_home_7
$ echo $JAVA_HOME
/usr/lib/jvm/java-7-oracle

How can I do these two steps in a single shot?

3
  • 2
    Why don't you merge the together?
    – cuonglm
    May 13, 2015 at 3:01
  • @cuonglm I originally didn't know how to merge sh and source.
    – Jin Kwon
    May 13, 2015 at 3:05
  • 3
    I mean move export command to the first script.
    – cuonglm
    May 13, 2015 at 3:06

2 Answers 2

1

If you don't want to merge the scripts, as in

#!/bin/sh

sudo \
    env JAVA_HOME=/usr/lib/jvm/java-7-oracle \
    update-java-alternatives -s java-7-oracle

you may use

#!/bin/sh

sudo \
    env BASH_ENV=path/to/java_home_7 \
    bash -c 'update-java-alternatives -s java-7-oracle'

This would cause the script to run update-java-alternatives in a bash -c in-line script started from env with sudo. The env utility sets the BASH_ENV environment variable to the pathname of the script that sets the JAVA_HOME environment variable. This causes the bash -c script to automatically source the other script before it calls the update-java-alternatives utility.

The use of env to set the environment variables makes sure that the variables are set even though sudo may initially clear the environment.

0

I'm not sure I'm aware of what I'm doing. Any comments or suggestions will be appreciated.

$ cat java7
#!/bin/sh
alternative=java-7-oracle
sudo update-java-alternatives -s $alternative
export JAVA_HOME=/usr/lib/jvm/$alternative

$ cat java8
#!/bin/sh
alternative=java-8-oracle
sudo update-java-alternatives -s $alternative
export JAVA_HOME=/usr/lib/jvm/$alternative

$ . java7;javac -version;echo $JAVA_HOME
javac 1.7.x_yy
/usr/lib/jvm/java-7-oracle

$ . java8;javac -version;echo $JAVA_HOME
javac 1.8.x_yy
/usr/lib/jvm/java-8-oracle

UPDATE

I'm updating for myself and anyone who needs.

First I added following line to my ~/.bashrc.

#http://serverfault.com/a/276221/113357
export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")

And my new ~/bin/java_home scripts.

#!/bin/bash
if [ $# -ne 1 ]
then
    echo please select a jname.
    update-java-alternatives -l
    return;
fi
jname=$1
sudo update-java-alternatives -s $jname
source ~/.bashrc

And when I want to change the JDK, I do.

$ source java_home java-7-oracle
$ source java_home java-8-oracle

The JAVA_HOME variable changes even in any new terminals. Previous java7 and java8 changed like this.

$ echo java7
#!/bin/sh
source java_home java-7-oracle

$ echo java8
#!/bin/sh
source java_home java-8-oracle

$
3
  • 2
    I take it you're using a Debian derivative. update-java-alternatives changes the /usr/lib/jvm/default-java symlink to point to the selected JVM, so you can set JAVA_HOME to /usr/lib/jvm/default-java once and for all. To switch JVMs you only need to run update-java-alternatives as appropriate, you don't need to change JAVA_HOME... May 13, 2015 at 4:35
  • Or just set JAVA_HOME and stop screwing with the system level alternatives. (which a non-priv user cannot do.)
    – Ricky
    May 13, 2015 at 5:26
  • I installed oracle-java7-installer and oracle-java8-installer via ppa:webupd8team/java. There is no default-java in /usr/lib/jvm/.
    – Jin Kwon
    May 27, 2015 at 4:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.