-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
install-java
executable file
·81 lines (69 loc) · 2.55 KB
/
install-java
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
#!/usr/local/bin/python3
# Copyright (c) KMG. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
##
import os
import subprocess
import jdk
import threading
JDK_VERSION = '17'
JDK_PATH = os.path.expanduser('~') + os.sep + 'jdk-' + JDK_VERSION
JAVA_HOME_DIR = os.sep + 'Contents' + os.sep + 'Home'
def install_java(res):
# Platform dependent install of Java JDK 17
os.system('mkdir -p '+JDK_PATH)
val = jdk.install(JDK_VERSION, path=JDK_PATH)
res.append(val)
def download_install_java():
# Obtains the platform dependent JDK download url
download_url = jdk.get_download_url(JDK_VERSION)
print("JDK Download URL: " + download_url)
print("Detected OS: " + jdk.OS) # Detected platform operating system
print("CPU type: " + jdk.ARCH) # Detected platform CPU architecture
lt = []
th = threading.Thread(target=install_java, args=(lt,))
print("Downloading and installing JDK " + JDK_VERSION + " ", end="", flush=True)
th.start()
while th.is_alive():
print(".", end="", flush=True)
try:
th.join(1)
except Exception as ex:
print(ex)
break
print()
if len(lt) > 0:
print("Successfully Installed JDK " + JDK_VERSION + " in the path: " + lt[0])
print("Set JAVA_HOME environment variable to " + lt[0] + JAVA_HOME_DIR)
else:
print("JDK " + JDK_VERSION + " installation failed!")
def get_java_version(text):
ret = " "
try:
out = text.split('"', 2)
ret = out[1].split('.')[0]
except Exception:
pass
return ret
def check_java_version():
process = subprocess.Popen(['java', '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True)
stdout, stderr = process.communicate()
val1, val2 = get_java_version(stdout), get_java_version(stderr)
ret = val1 >= JDK_VERSION or val2 >= JDK_VERSION
if ret:
print("stdout Java Major version: " + val1)
print("stderr Java Major version: " + val2)
return ret
if __name__ == "__main__":
if check_java_version():
print("JDK version: " + JDK_VERSION + " or higher already found in this system. Skipping JDK "
"installation!")
else:
print("Downloading and installing JDK Version: " + JDK_VERSION)
download_install_java()