-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
81 lines (60 loc) · 2.75 KB
/
main.py
File metadata and controls
81 lines (60 loc) · 2.75 KB
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
import subprocess
import os
import ctypes
if os.name.lower() != 'nt':
print('This only works on Windows.\nI recommend SDKMAN for Linux/macOS.')
exit(1)
def isAdmin():
return ctypes.windll.shell32.IsUserAnAdmin()
def setJavaHome(javaVersion, vendor, vendors):
sysPath = [env for env in os.environ['PATH'].split(
os.pathsep) if 'jre' not in env and 'jdk' not in env]
usrPath = [p for p in subprocess.check_output(['powershell', '-Command', '[Environment]::GetEnvironmentVariable("PATH", "User")']
).decode('utf-8').split(os.pathsep) if 'jre' not in p.lower() and 'jdk' not in p.lower()]
for type_ in ('jre', 'jdk'):
path = vendors[vendor].format(version=javaVersion, type=type_)
if os.path.exists(path):
sysPath = [os.path.join(path, "bin")] + sysPath
usrPath = [os.path.join(path, "bin")] + usrPath
print(f"Setting {path} as default version...")
subprocess.run(
['powershell', '-Command',
f'New-ItemProperty -Path "HKLM:SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment" -Name JAVA_HOME -Value "{path}" -PropertyType String -Force'],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print('Set user JAVA_HOME variable.')
subprocess.run([
'powershell', '-Command',
f'New-ItemProperty -Path "HKLM:SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment" -Name PATH -Value "{os.pathsep.join(sysPath)}" -PropertyType String -Force'],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print('Set system PATH variable.')
subprocess.run([
'powershell', '-Command',
f'[Environment]::SetEnvironmentVariable("PATH", "{os.pathsep.join(usrPath)}", "User")'
])
print('Set user PATH variable.')
return True
return False
if isAdmin():
javaVersions = {
'8', '11', '17', '21', '25'
}
vendors = {
'Oracle': 'C:/Program Files/Java/{type}-{version}',
'Adoptium': 'C:/Program Files/Eclipse Adoptium/{type}-{version}',
'Zulu': 'C:/Program Files/Zulu/zulu-{version}-{type}'
}
while True:
javaVersion = input('Input Java Version: ')
if javaVersion in javaVersions:
break
else:
print(f'Invalid version.\n{", ".join(javaVersions)}')
while True:
vendor = input('Input Java Vendor: ')
if vendor in vendors:
break
else:
print(f'Invalid vendor.\n{", ".join(vendors.keys())}')
setJavaHome(javaVersion, vendor, vendors)
else:
print('Please run as an administrator.')