Shell script for set java home path on linux
Save this script and run , you will see this screen:
Choose the first option and past the path for your jdk folder:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
### BEGIN INIT INFO
# SCRIPT TO SET JAVA_HOME ON LINUX
### END INIT INFO</code>
# Author: Samuel Teixeira samuelteixeiras[at]gmail.com
next=menu
# loop principal
while : ; do
case "$next" in
menu)
resposta=$(
dialog --stdout \
--title 'Menu' \
--menu 'Select one option:' \
0 0 0 \
1 'Set Java Path.'\
0 'Exit' )
# CANCELAR or ESC, exir...
[ $? -ne 0 ] && break
# De acordo com a opção escolhida, dispara programas
case "$resposta" in
1)
next=setPath
back=menu
;;
0) break ;;
esac
;;
setPath)
next=$back
dir=$(dialog --stdout \
--title 'Set java Path' \
--inputbox 'Please, Insert the name of java path \n Ex: /usr/local/jdk1.8.0:' \
0 0)
bashFile=~/.bashrc
if [ -d "$dir" ]
then
echo "export JAVA_HOME=$dir" >> $bashFile
echo "export PATH=$PATH:$dir/bin" >> $bashFile
else
echo "$dir not found."
fi
source $bashFile
sudo update-alternatives --install "/usr/bin/java" "java" "$dir/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "$dir/bin/javac" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "$dir/bin/javaws" 1
sudo chmod a+x /usr/bin/java
sudo chmod a+x /usr/bin/javac
sudo chmod a+x /usr/bin/javaws
sudo chown -R root:root $dir
break
;;
*)
echo "Bad Window '$next'."
echo Abort programm...
exit
esac
# Aqui é feito o tratamento genérico de Código de Retorno
# de todas as telas. Volta para a tela back se for
# CANCELAR, sai do programa se for ESC.
retorno=$?
[ $retorno -eq 1 ] && next=$back # cancel
[ $retorno -eq 255 ] && break # Esc
done
clear
sudo update-alternatives --config java
java -version