alternatives --config java bash script
You can run the alternatives
command non-interactively too. Instead of --config
, use the --set
option to specify the path of the alternative directly.
sudo alternatives --set java /location/of/jdk1.6/bin/java
Generally, you can feed any program that expects something on the standard input like this:
echo -e "line 1\nline 2\nline 3" | program
I did it using this script:
tmp=`mktemp`
echo 2 > $tmp
alternatives --config java < $tmp
rm -f $tmp
The <
means that the content of the $tmp
file will be passed to the input of the alternatives command.
Edit: You could simply use a single pipe as other suggested:
echo 2 | sudo alternatives --config java
This worked for me with Java 8:
alternatives --install /usr/bin/java java /usr/lib/jvm/jre1.8.0_60/bin/java 3
alternatives --config java <<< '3'