GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Support AdoptOpenJDK · KanishGH/install-java@63997dc

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • README.md
  • install-java.sh
  • uninstall-java.sh
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎README.md‎

Copy file name to clipboard
+40-44Lines changed: 40 additions & 44 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
Oracle Java installation script for Linux
2-
=========================================
1+
Java Development Kit (JDK) installation script for Linux
2+
========================================================
33

4-
"install-java.sh" is an installation script for setting up Oracle Java Development Kit on Debian based Linux Operating Systems.
4+
"install-java.sh" is an installation script for setting up Java Development Kit (JDK) on Debian based Linux Operating Systems.
5+
6+
Currently, the `install-java.sh` script supports `tar.gz` distributions from [Oracle](https://www.oracle.com/technetwork/java/index.html), [OpenJDK](http://openjdk.java.net/) and [AdoptOpenJDK](https://adoptopenjdk.net/)
57

68
I'm mainly using Ubuntu and therefore this script is tested only on Ubuntu versions.
79

810
## Prerequisites
911

10-
The script uses "unzip" command. Therefore please make sure it is installed.
12+
The script uses "unzip" command. Therefore, please make sure it is installed.
1113

1214
`sudo apt install unzip`
1315

14-
The "install-java.sh" script will not download the Java distribution. You need to [download JDK] from Oracle.
16+
The "install-java.sh" script will not download the Java distribution. You must download JDK `tar.gz` distribution.
1517

16-
It is required to have all Java distributions in a single directory.
18+
For Oracle JDK 7 & 8, `install-java.sh` supports installing demos and "Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files". This files are picked up from the same directory.
1719

1820
For example, if you want to install Java 7, following files should be downloaded and moved to a single directory.
1921

@@ -23,57 +25,50 @@ For example, if you want to install Java 7, following files should be downloaded
2325

2426
Similarly for Java 8, following are the files required
2527

26-
- jdk-8u172-linux-x64.tar.gz
27-
- jdk-8u172-linux-x64-demos.tar.gz
28+
- jdk-8u212-linux-x64.tar.gz
29+
- jdk-8u212-linux-x64-demos.tar.gz
2830
- jce_policy-8.zip
2931

30-
For Java 9 and 10, you only need to have the Java binary distribution. For example,
31-
32-
- jdk-10.0.1_linux-x64_bin.tar.gz
33-
34-
The Java Demos and Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy files are optional.
32+
The Java Demos and Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy files are optional. There are no demos from Java 9 upwards. Since Java 9, default JCE policy files already allow for "unlimited" cryptographic strengths.
3533

36-
There are no demos from Java 9 upwards.
37-
38-
Since Java 9, default JCE policy files already allow for "unlimited" cryptographic strengths.
34+
For any other JDK, you only need to have the Java binary distribution.
3935

4036
## Installation
4137

4238
The script needs to be run as root.
4339

44-
You need to provide the JDK distribution file (tar.gz) and the Java Installation Directory. The default value for Java installation directory is "/usr/lib/jvm"
45-
46-
```
47-
Usage:
48-
install-java.sh -f <java_dist> [-p] <java_dir>
49-
50-
-f: The jdk tar.gz file
51-
-p: Java installation directory
52-
```
40+
You need to provide the JDK distribution file (`tar.gz`) and the Java Installation Directory. The default value for Java installation directory is "/usr/lib/jvm"
5341

54-
Example: Install Oracle JDK 7
42+
```console
43+
$ sudo ./install-java.sh -h
5544

56-
`sudo ./install-java.sh -f ~/software/java/jdk-7u80-linux-x64.tar.gz`
57-
58-
Example: Install Oracle JDK 8
59-
60-
`sudo ./install-java.sh -f ~/software/java/jdk-8u172-linux-x64.tar.gz`
45+
Usage:
46+
install-java.sh -f <java_dist> [-p <java_dir>]
6147

62-
Example: Install Oracle JDK 9
48+
-f: The jdk tar.gz file.
49+
-p: Java installation directory. Default: /usr/lib/jvm.
50+
-h: Display this help and exit.
6351

64-
`sudo ./install-java.sh -f ~/software/java/jdk-9.0.1_linux-x64_bin.tar.gz`
52+
```
6553

66-
Example: Install Oracle JDK 10
54+
Example: Installing Oracle JDK 12
6755

68-
`sudo ./install-java.sh -f ~/software/java/jdk-10.0.1_linux-x64_bin.tar.gz`
56+
```console
57+
$ sudo ./install-java.sh -f ~/software/java/jdk-12.0.1_linux-x64_bin.tar.gz
58+
Installing: jdk-12.0.1_linux-x64_bin.tar.gz
59+
Extracting /home/isuru/software/java/jdk-12.0.1_linux-x64_bin.tar.gz to /usr/lib/jvm
60+
JDK is extracted to /usr/lib/jvm/jdk-12.0.1
61+
Run update-alternatives commands? [y/N]
62+
Do you want to set JAVA_HOME environment variable in /home/isuru/.bashrc? [y/N]
63+
```
6964

7065
## Automate Java Installation
7166

7267
You can automate the Java installation script by using the `yes` command.
7368

74-
Example: Install Oracle JDK 8
69+
Example: Installing AdoptOpenJDK 11
7570

76-
`yes | sudo ./install-java.sh -f ~/software/java/jdk-8u172-linux-x64.tar.gz`
71+
`yes | sudo ./install-java.sh -f ~/software/java/OpenJDK11U-jdk_x64_linux_hotspot_11.0.3_7.tar.gz`
7772

7873
## Java Mission Control
7974

@@ -85,25 +80,26 @@ If the "Welcome" page is blank in JMC, try installing `libwebkitgtk-3.0-0` packa
8580

8681
## Uninstallation
8782

88-
There is another script named "uninstall-java.sh" to uninstall Java.
83+
There is another script named "uninstall-java.sh" to uninstall Java.
8984

9085
You need to provide Java distribution directory.
9186

92-
```
87+
```console
88+
$ sudo ./uninstall-java.sh -h
89+
9390
Usage:
9491
uninstall-java.sh -p <java_dist_dir>
9592

96-
-p: Java distribution directory
93+
-p: Java distribution directory.
94+
-h: Display this help and exit.
9795
```
9896

99-
Example: Uninstall Oracle JDK 7
97+
Example: Uninstall Oracle JDK 12
10098

101-
`sudo ./uninstall-java.sh -p /usr/lib/jvm/jdk1.7.0_80/`
99+
`sudo ./uninstall-java.sh -p /usr/lib/jvm/jdk-12.0.1/`
102100

103101
## License
104102

105103
Copyright (C) 2014 M. Isuru Tharanga Chrishantha Perera
106104

107105
Licensed under the Apache License, Version 2.0
108-
109-
[download JDK]: http://www.oracle.com/technetwork/java/javase/downloads/index.html
Collapse file

‎install-java.sh‎

Copy file name to clipboardExpand all lines: install-java.sh
+62-55Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,21 @@
1818
# ----------------------------------------------------------------------------
1919

2020
java_dist=""
21-
java_dir=""
21+
default_java_dir="/usr/lib/jvm"
22+
java_dir="$default_java_dir"
2223

23-
function help() {
24+
function usage() {
2425
echo ""
2526
echo "Usage: "
26-
echo "install-java.sh -f <java_dist> [-p] <java_dir>"
27+
echo "install-java.sh -f <java_dist> [-p <java_dir>]"
2728
echo ""
28-
echo "-f: The jdk tar.gz file"
29-
echo "-p: Java installation directory"
29+
echo "-f: The jdk tar.gz file."
30+
echo "-p: Java installation directory. Default: $default_java_dir."
31+
echo "-h: Display this help and exit."
3032
echo ""
3133
}
3234

33-
confirm() {
35+
function confirm() {
3436
# call with a prompt string or use a default
3537
read -r -p "${1:-Are you sure?} [y/N] " response
3638
case $response in
@@ -50,93 +52,101 @@ if [ "$UID" -ne "0" ]; then
5052
exit 9
5153
fi
5254

53-
while getopts "f:p:" opts; do
55+
while getopts "f:p:h" opts; do
5456
case $opts in
5557
f)
5658
java_dist=${OPTARG}
5759
;;
5860
p)
5961
java_dir=${OPTARG}
6062
;;
63+
h)
64+
usage
65+
exit 0
66+
;;
6167
\?)
62-
help
68+
usage
6369
exit 1
6470
;;
6571
esac
6672
done
6773

6874
if [[ ! -f $java_dist ]]; then
69-
echo "Please specify the Java distribution file (tar.gz)"
70-
help
75+
echo "Please specify the Java distribution file."
76+
exit 1
77+
fi
78+
79+
# Validate Java Distribution
80+
java_dist_filename=$(basename $java_dist)
81+
82+
if [[ ${java_dist_filename: -7} != ".tar.gz" ]]; then
83+
echo "Java distribution must be a valid tar.gz file."
7184
exit 1
7285
fi
7386

7487
#Check whether unzip command exsits
7588
if ! command -v unzip >/dev/null 2>&1; then
76-
echo "Please install unzip (apt -y install unzip)"
89+
echo "Please install unzip (apt -y install unzip)."
7790
exit 1
7891
fi
7992

80-
#If no directory was provided, we need to create the default one
81-
if [[ -z $java_dir ]]; then
82-
java_dir="/usr/lib/jvm"
93+
# Create the default directory if user has not specified any other path
94+
if [[ $java_dir == $default_java_dir ]]; then
8395
mkdir -p $java_dir
8496
fi
8597

8698
#Validate java directory
8799
if [[ ! -d $java_dir ]]; then
88-
echo "Please specify a valid Java installation directory"
100+
echo "Please specify a valid Java installation directory."
89101
exit 1
90102
fi
91103

92-
# Validate Java Distribution
93-
java_dist_filename=$(basename $java_dist)
94-
echo "Installing $java_dist_filename"
95-
96-
java_78_dist_file_regex="^jdk-([78])u([0-9]{1,3})-linux-(i586|x64)\.tar\.gz$"
97-
java_9up_dist_file_regex="^(open)?jdk-([91][0-9]?\.?[0-9]*\.?[0-9]*)_linux-x(32|64)_bin\.tar\.gz$"
104+
echo "Installing: $java_dist_filename"
98105

99-
# JDK Directory with version (tar -tzf $java_dist | head -1 | cut -f1 -d"/")
100-
jdk_dir=""
101-
# JDK Major Version
102-
jdk_major_version=""
106+
# Check Java executable
107+
java_exec="$(tar -tzf $java_dist | grep ^[^/]*/bin/java$ || echo "")"
103108

104-
if [[ $java_dist_filename =~ $java_78_dist_file_regex ]]; then
105-
jdk_dir=$(echo $java_dist_filename | sed -nE "s/$java_78_dist_file_regex/jdk1.\1.0_\2/p")
106-
jdk_major_version=$(echo $jdk_dir | sed -nE 's/jdk1\.([0-9]*).*/\1/p')
107-
elif [[ $java_dist_filename =~ $java_9up_dist_file_regex ]]; then
108-
jdk_dir=$(echo $java_dist_filename | sed -nE "s/$java_9up_dist_file_regex/jdk-\2/p")
109-
jdk_major_version=$(echo $jdk_dir | sed -nE 's/jdk-([0-9]*).*/\1/p')
110-
else
111-
echo "Please specify a valid Java distribution"
109+
if [[ -z $java_exec ]]; then
110+
echo "Could not find \"java\" executable in the distribution. Please specify a valid Java distribution."
112111
exit 1
113112
fi
114113

114+
# JDK Directory with version
115+
jdk_dir="$(echo $java_exec | cut -f1 -d"/")"
115116
extracted_dirname=$java_dir"/"$jdk_dir
116117

117118
# Extract Java Distribution
118-
119119
if [[ ! -d $extracted_dirname ]]; then
120120
echo "Extracting $java_dist to $java_dir"
121121
tar -xof $java_dist -C $java_dir
122122
echo "JDK is extracted to $extracted_dirname"
123123
else
124-
echo "WARN: JDK was not extracted to $extracted_dirname. The path already exists."
124+
echo "WARN: JDK was not extracted to $java_dir. There is an existing directory with name $jdk_dir."
125+
exit 1
125126
fi
126127

127-
if [[ ! -f $extracted_dirname"/bin/java" ]]; then
128+
if [[ ! -f "${extracted_dirname}/bin/java" ]]; then
128129
echo "ERROR: The path $extracted_dirname is not a valid Java installation."
129130
exit 1
130131
fi
131132

132-
# print "Java version"
133-
echo "------------------------------------------------------------------------------"
134-
${extracted_dirname}/bin/java -version
135-
echo "------------------------------------------------------------------------------"
133+
# Oracle JDK: 7 to 8
134+
java_78_dir_regex="^jdk1\.([0-9]*).*$"
135+
# Oracle JDK / OpenJDK / AdoptOpenJDK: 9 and upwards
136+
java_9up_dir_regex="^jdk-([0-9]*).*$"
137+
138+
# JDK Major Version
139+
jdk_major_version=""
140+
141+
if [[ $jdk_dir =~ $java_78_dir_regex ]]; then
142+
jdk_major_version=$(echo $jdk_dir | sed -nE "s/$java_78_dir_regex/\1/p")
143+
else
144+
jdk_major_version=$(echo $jdk_dir | sed -nE "s/$java_9up_dir_regex/\1/p")
145+
fi
136146

137147
# Install Demos
138148

139-
if [[ $java_dist_filename =~ $java_78_dist_file_regex ]]; then
149+
if [[ $jdk_dir =~ $java_78_dir_regex ]]; then
140150
# Demos are only available for Java 7 and 8
141151
demos_dist=$(dirname $java_dist)"/"$(echo $java_dist_filename | sed 's/\.tar\.gz/-demos\0/')
142152
fi
@@ -154,9 +164,9 @@ fi
154164

155165
unlimited_jce_policy_dist=""
156166

157-
if [[ "$java_dist_filename" =~ ^jdk-7.* ]]; then
167+
if [[ $jdk_dir =~ ^jdk1\.7.* ]]; then
158168
unlimited_jce_policy_dist="$(dirname $java_dist)/UnlimitedJCEPolicyJDK7.zip"
159-
elif [[ "$java_dist_filename" =~ ^jdk-8.* ]]; then
169+
elif [[ $jdk_dir =~ ^jdk1\.8.* ]]; then
160170
unlimited_jce_policy_dist="$(dirname $java_dist)/jce_policy-8.zip"
161171
fi
162172

@@ -168,17 +178,14 @@ if [[ -f $unlimited_jce_policy_dist ]]; then
168178
fi
169179

170180
# Run update-alternatives commands
171-
172-
commands=("jar" "java" "javac" "javadoc" "javah" "javap" "javaws" "jcmd" "jconsole" "jarsigner" "jhat" "jinfo" "jmap" "jmc" "jps" "jstack" "jstat" "jstatd" "jvisualvm" "keytool" "policytool" "wsgen" "wsimport")
173-
174181
if (confirm "Run update-alternatives commands?"); then
175-
echo "Running update-alternatives --install and --config for ${commands[@]} mozilla-javaplugin.so"
176-
177-
for i in "${commands[@]}"; do
178-
command_path=$extracted_dirname/bin/$i
179-
if [[ -f $command_path ]]; then
180-
update-alternatives --install "/usr/bin/$i" "$i" "$command_path" 10000
181-
update-alternatives --set "$i" "$command_path"
182+
echo "Running update-alternatives..."
183+
declare -a commands=($(ls -1 ${extracted_dirname}/bin))
184+
for command in "${commands[@]}"; do
185+
command_path=$extracted_dirname/bin/$command
186+
if [[ -x $command_path ]]; then
187+
update-alternatives --install "/usr/bin/$command" "$command" "$command_path" 10000
188+
update-alternatives --set "$command" "$command_path"
182189
fi
183190
done
184191

@@ -192,14 +199,14 @@ fi
192199
# Create system preferences directory
193200
java_system_prefs_dir="/etc/.java/.systemPrefs"
194201
if [[ ! -d $java_system_prefs_dir ]]; then
195-
if (confirm "Create Java System Prefs Directory and change ownership to $SUDO_USER:$SUDO_USER?"); then
202+
if (confirm "Create Java System Prefs Directory ($java_system_prefs_dir) and change ownership to $SUDO_USER:$SUDO_USER?"); then
196203
echo "Creating $java_system_prefs_dir"
197204
mkdir -p $java_system_prefs_dir
198205
chown -R $SUDO_USER:$SUDO_USER $java_system_prefs_dir
199206
fi
200207
fi
201208

202-
if (confirm "Do you want to set JAVA_HOME environment variable?"); then
209+
if (confirm "Do you want to set JAVA_HOME environment variable in $HOME/.bashrc?"); then
203210
if grep -q "export JAVA_HOME=.*" $HOME/.bashrc; then
204211
sed -i "s|export JAVA_HOME=.*|export JAVA_HOME=$extracted_dirname|" $HOME/.bashrc
205212
else

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.