This repository was archived by the owner on Apr 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava.ps1
More file actions
63 lines (52 loc) · 1.38 KB
/
Copy pathjava.ps1
File metadata and controls
63 lines (52 loc) · 1.38 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
PARAM (
[parameter(Mandatory=$false)][int] $choiceNum
)
. "$PSScriptRoot\scripts\find-exe.ps1"
$exeLocs = @(
"Oracle/*jdk*",
"Oracle/*jre*",
"*Adoptium*/*jdk*",
"*Adoptium*/*jre*",
"*AdoptOpenJDK*/*jdk*",
"*AdoptOpenJDK*/*jre*"
)
function Find-AllJava() {
return Find-Defaults $exeLocs
}
function Show-Help() {
Write-Host "Usage: activate-java [choiceNum]`n"
Write-Host -NoNewline "Example: "
Write-Host -ForegroundColor Magenta "activate-java 1`n"
Write-Host "Available:"
$results = Find-AllJava
if ($results.Length -gt 0) {
$results | % {$i = 0} {
Write-Host "`t$($i):`t $_"
$i++
}
Write-Host "`n"
} else {
Write-Host "-"
}
}
function Activate($loc) {
Write-Host -NoNewline "Setting `$env to "
Write-Host -ForegroundColor Magenta "$loc`n"
$binPath = Join-Path $loc -ChildPath bin
# Set Path
$Env:JAVA_HOME = "$loc"
$Env:PATH = "$binPath;$Env:PATH"
}
## =========== START ===========
IF (!$PSBoundParameters.ContainsKey('choiceNum')) {
Show-Help
RETURN
}
$all_paths = Find-AllJava
IF ($choiceNum -ge $all_paths.length) {
Write-Host -ForegroundColor Red "[ERROR] Given choice $choiceNum is out of range!`n"
RETURN
}
$java_path = $all_paths[$choiceNum]
Activate $java_path
RETURN