forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet-JavaVersion.ps1
More file actions
165 lines (146 loc) · 6.34 KB
/
Set-JavaVersion.ps1
File metadata and controls
165 lines (146 loc) · 6.34 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Use case:
#
# The purpose of this script is to enable easier switching of the JDK being used as the JAVA_HOME in a CLI process.
#
# There are two ways of running this script:
#
# - Provide the path of the JDK to use.
# - Allow the script to find Azul and Eclipse JDKs using the registry keys on the machine.
#
# This script won't modify the User or Machine environment variables, only the Process environment variables.
# Additionally, because this uses registry keys and modifies Process environment variables this script is only usable
# on Windows. Linux and macOs users should use the tooling available on those OSes to switch the JAVA_HOME for the
# running process.
param(
[Parameter(Mandatory=$false)][string]$JdkPath
)
if (!$IsWindows) {
Write-Error "This script is only usable on Windows."
exit 1
}
if ("" -eq $JdkPath) {
# Use the HKEY_LOCAL_MACHINE registry keys to find JDK installs from Azul and Eclipse.
$registryBase = "HKLM:\SOFTWARE"
$azulJdks = "$registryBase\Azul Systems\Zulu"
$eclipseJdks = "$registryBase\Eclipse Adoptium\JDK"
$eclipseFoundationJdks = "$registryBase\Eclipse Foundation\JDK"
$microsoftJdks = "$registryBase\Microsoft\JDK"
# Collect all available JDK installations in the following format
#
# Option # | <Distributor> | <Major Version> | <Full Version> | <Install Location>
#
# For example
#
# 1 | Zulu | 17 | 17.28.13 | C:\Program Files\Zulu\zulu-17
#
# Then let the caller of the script select which version they want to make the new JAVA_HOME,
# if any version. Choosing not to select a new version will leave the current JAVA_HOME
# unmodified.
[System.Data.DataTable]$jdkOptions = New-Object System.Data.Datatable
[void]$jdkOptions.Columns.Add("Choice")
[void]$jdkOptions.Columns.Add("Distributor")
[void]$jdkOptions.Columns.Add("Major Version")
[void]$jdkOptions.Columns.Add("Full Version")
[void]$jdkOptions.Columns.Add("Install Location")
Write-Host "Searching for JDK installs from Azul and Eclipse"
$choiceNumber = 1
# Azul uses a registry key pattern of
#
# <Install Name> (ex zulu-17)
# - \ <CurrentVersion> (ex 17.28.13)
# <MajorVersion> (ex 17)
# <InstallationPath> (ex C:\Progam Files\Zulu\zulu-17\)
#
# There are additional properties that aren't needed and aren't included above.
#
# Select the installs in the following format
#
# Zulu | <MajorVersion> | <CurrentVersion> | <InstallationPath>
if (Test-Path -Path $azulJdks) {
foreach ($jdk in Get-ChildItem -Path $azulJdks) {
$jdkHKeyPath = $jdk.Name
$values = Get-ItemProperty -Path "Registry::$jdkHKeyPath"
[void]$jdkOptions.Rows.Add($choiceNumber, "Zulu", $values.MajorVersion, $values.CurrentVersion, $values.InstallationPath)
$choiceNumber++
}
}
# Eclipse uses a registry key pattern of
#
# <Current Version> (ex 17.0.4.8)
# - \ <Implementation> (ex hotspot)
# - \ <MSI>
# - \ <Path> (ex C:\Program Files\Eclipse Adoptium\jdk-17.0.4.8-hotspot\)
#
# There are additional properties that aren't needed and aren't included above.
#
# Select the installs in the following format
#
# Eclipse | <First number segment in Current Version> | <Current Version> | <Path>
if (Test-Path -Path $eclipseJdks) {
foreach ($jdk in Get-ChildItem -Path $eclipseJdks) {
$jdkHKeyPath = $jdk.Name
$jdkFullVersion = $jdk.PSChildName
$jdkChoicePath = (Get-ItemProperty -Path "Registry::$jdkHKeyPath\hotspot\MSI").Path
[void]$jdkOptions.Rows.Add($choiceNumber, "Eclipse", $jdkFullVersion.split(".")[0], $jdkFullVersion, $jdkChoicePath)
$choiceNumber++
}
}
if (Test-Path -Path $eclipseFoundationJdks) {
foreach ($jdk in Get-ChildItem -Path $eclipseFoundationJdks) {
$jdkHKeyPath = $jdk.Name
$jdkFullVersion = $jdk.PSChildName
$jdkChoicePath = (Get-ItemProperty -Path "Registry::$jdkHKeyPath\hotspot\MSI").Path
[void]$jdkOptions.Rows.Add($choiceNumber, "Eclipse", $jdkFullVersion.split(".")[0], $jdkFullVersion, $jdkChoicePath)
$choiceNumber++
}
}
# Microsoft uses a registry key pattern of
#
# <Current Version> (ex 17.0.7.7)
# - \ <Implementation> (ex hotspot)
# - \ <MSI>
# - \ <Path> (ex C:\Program Files\Microsoft\jdk-17.0.7.7-hotspot\)
#
# There are additional properties that aren't needed and aren't included above.
#
# Select the installs in the following format
#
# Microsoft | <First number segment in Current Version> | <Current Version> | <Path>
if (Test-Path -Path $microsoftJdks) {
foreach ($jdk in Get-ChildItem -Path $microsoftJdks) {
$jdkHKeyPath = $jdk.Name
$jdkFullVersion = $jdk.PSChildName
$jdkChoicePath = (Get-ItemProperty -Path "Registry::$jdkHKeyPath\hotspot\MSI").Path
[void]$jdkOptions.Rows.Add($choiceNumber, "Microsoft", $jdkFullVersion.split(".")[0], $jdkFullVersion, $jdkChoicePath)
$choiceNumber++
}
}
$choiceNumber--
$selection = $null
while ($null -eq $selection) {
$jdkOptions | Format-Table -AutoSize
$selection = Read-Host "Select the JDK to set (or keep current JDK by entering no option)"
if ($null -ne $selection) {
if ($selection.Trim() -eq "") {
break
}
$selectionNumber = $selection -as [int]
if ($null -eq $selectionNumber -or $selectionNumber -lt 1 -or $selectionNumber -gt $choiceNumber) {
Write-Host "Invalid selection '$selectionNumber', choose a choice from 1 to $choiceNumber"
$selection = $null
} else {
$JdkPath = $jdkOptions.Rows[$selection - 1]["Install Location"]
if ($JdkPath.EndsWith("\")) {
$JdkPath = $JdkPath.Substring(0, $JdkPath.Length - 1)
}
}
}
}
}
if ("" -ne $JdkPath) {
Write-Host "Beginning replacement of process environment variables"
Write-Host "Replacing current JAVA_HOME '$env:JAVA_HOME' with new JAVA_HOME '$JdkPath'"
$env:JAVA_HOME = $JdkPath
Write-Host "Setting '$JdkPath\bin' to the first value in Path"
$env:Path = "$JdkPath\bin;$env:Path"
}