forked from JustOneMoreBlock/shell-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_java.sh
138 lines (127 loc) · 5.09 KB
/
install_java.sh
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash
################################################################################
##
## Title: Oracle Java Installer
##
## Date: 03/27/2015
## Author: Mike G. aka metalcated and partially forked from n0ts
## Version: 0.2
##
## Changelog: 0.1 - Initial Release
## 0.2 - Fixed dupe download issue
##
## Usage: ./install_java.sh <jre|jdk_version> <rpm|tar>
##
## Defaults: jre|jdk_version: 8 / rpm
##
################################################################################
ADMIN_EMAIL=""
# type can be jre or jdk
JAVA_TYPE="jre"
JAVA_VERSION="8"
EXT="rpm"
# set default if suggested
if [[ -n "$1" ]]; then
if [[ "$1" == "7" ]]; then
JAVA_VERSION="7"
fi
fi
# set download extension
if [[ -n "$2" ]]; then
if [[ "$2" == "tar" ]]; then
EXT="tar.gz"
fi
fi
# set base download location
URL="https://www.oracle.com"
DOWNLOAD_URL1="${URL}/technetwork/java/javase/downloads/index.html"
DOWNLOAD_URL2=$(curl -s $DOWNLOAD_URL1 | egrep -o "\/technetwork\/java/\javase\/downloads\/${JAVA_TYPE}${JAVA_VERSION}-downloads-.*\.html" | head -1)
# check to make sure we got to oracle
if [[ -z "$DOWNLOAD_URL2" ]]; then
echo "Could not to oracle - $DOWNLOAD_URL1"
exit 1
fi
# set download url
DOWNLOAD_URL3="$(echo ${URL}${DOWNLOAD_URL2}|awk -F\" {'print $1'})"
DOWNLOAD_URL4=$(curl -s "$DOWNLOAD_URL3" | egrep -o "https\:\/\/download.oracle\.com\/otn-pub\/java\/jdk\/[7-8]u[0-9]+\-(.*)+\/${JAVA_TYPE}-[7-8]u[0-9]+(.*)linux-x64.${EXT}"|tail -n1)
# check to make sure url exists
if [[ -z "$DOWNLOAD_URL4" ]]; then
echo "Could not get ${JAVA_TYPE} download url - $DOWNLOAD_URL4"
exit 1
fi
# set download file name
JAVA_INSTALL=$(basename $DOWNLOAD_URL4)
if [[ "$EXT" == "rpm" ]]; then
# download java
echo -e "\n\e[32mDownloading\e[0m: $DOWNLOAD_URL4"
while true;
do echo -n .;sleep 1;done &
cd /tmp; wget -q --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie" $DOWNLOAD_URL4 -O $JAVA_INSTALL > /dev/null 2>&1
kill $!; trap 'kill $!' SIGTERM;
# install rpm
echo -e "\n\e[32mInstalling\e[0m: $JAVA_INSTALL\r"
while true;
do echo -n .;sleep 1;done &
rpm -Uvh /tmp/$JAVA_INSTALL > /dev/null 2>&1
kill $!; trap 'kill $!' SIGTERM;
echo -e "\n\e[32mInstall\e[0m Complete\n"
# get dirname
JAVA_DIR=$(ls -tr /usr/java/|grep ${JAVA_TYPE}|head -n 1)
# set temp env var
export JAVA_HOME=/usr/java/${JAVA_DIR}
# set perm env var
echo "export JAVA_HOME=/usr/java/${JAVA_DIR}" >> /etc/environment
# set if jdk is used
if [[ "$JAVA_TYPE" = "jdk" ]]; then
# set temp env var
export JRE_HOME=/usr/java/${JAVA_DIR}/jre
# set perm env var
echo "export JRE_HOME=/usr/java/${JAVA_DIR}/${JAVA_TYPE}" >> /etc/environment
fi
# make sure java installed
ls /usr/java/${JAVA_DIR} > /dev/null 2>&1
if [[ "$?" != 0 ]]; then
echo -e "\n\e[31mError\e[0m: Java does not seem to be installed correctly,\nPlease try again or email admin: ${ADMIN_EMAIL}\n"
exit 1
fi
elif [[ "$EXT" == "tar" || "$EXT" == "tar.gz" ]]; then
# download java
echo -e "\n\e[32mDownloading\e[0m: $DOWNLOAD_URL4"
while true;
do echo -n .;sleep 1;done &
cd /opt; wget --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie" $DOWNLOAD_URL4 -O $JAVA_INSTALL > /dev/null 2>&1
kill $!; trap 'kill $!' SIGTERM;
# extract the tar
echo -e "\n\e[32mExtracting\e[0m: $JAVA_INSTALL\r"
while true;
do echo -n .;sleep 1;done &
tar xzf $JAVA_INSTALL > /dev/null 2>&1
kill $!; trap 'kill $!' SIGTERM;
echo -e "\n\e[32mInstall\e[0m Complete\n"
# get dirname
JAVA_DIR=$(ls -tr /opt/|grep ${JAVA_TYPE}|head -n 1)
# set default java
alternatives --install /usr/bin/java java /opt/${JAVA_DIR}/bin/java 1
alternatives --install /usr/bin/javac javac /opt/${JAVA_DIR}/bin/javac 1
alternatives --install /usr/bin/jar jar /opt/${JAVA_DIR}/bin/jar 1
# set temp env vars
export JAVA_HOME=/opt/${JAVA_DIR}
export PATH=$PATH:/opt/${JAVA_DIR}/bin:/opt/${JAVA_DIR}/${JAVA_TYPE}/bin
# set perm env vars
echo "export JAVA_HOME=/opt/${JAVA_DIR}" >> /etc/environment
echo "export PATH=$PATH:/opt/${JAVA_DIR}/bin:/opt/${JAVA_DIR}/${JAVA_TYPE}/bin" >> /etc/environment
# set if jdk is used
if [[ "$JAVA_TYPE" = "jdk" ]]; then
# set temp env var
export JRE_HOME=/opt/${JAVA_DIR}/${JAVA_TYPE}
# set perm env var
echo "export JRE_HOME=/opt/${JAVA_DIR}/${JAVA_TYPE}" >> /etc/environment
fi
# make sure java installed
ls /opt/${JAVA_DIR} > /dev/null 2>&1
if [[ "$?" != 0 ]]; then
echo -e "\n\e[31mError\e[0m: Java does not seem to be installed correctly,\nPlease try again or email admin: ${ADMIN_EMAIL}\n"
exit 1
fi
fi
# end script