-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChangeJdkVersion.psm1
69 lines (62 loc) · 2.49 KB
/
ChangeJdkVersion.psm1
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
function Out-Cache {
$data = (Get-ChildItem -Path (Get-PSDrive -PSProvider FileSystem).Root -Recurse -Filter "javac.exe" -ErrorAction SilentlyContinue).FullName
$jdkPaths = $data
Write-Output "---------------List Cached JDK Path----------"
for ( $index = 0; $index -lt $data.count; $index++ ) {
Write-Host $data[$index] -ForegroundColor Red
$node = $data[$index]
$loc = $node.LastIndexOf("\") - 1
$firstNode = $node.Substring(0, $loc)
$loc = $firstNode.LastIndexOf("\")
$jdkPaths[$index] = $firstNode.Substring(0, $loc)
}
Out-File -FilePath .\jdkpath.txt -InputObject $jdkPaths
if (Test-Path -Path "jdkpath.txt" -PathType leaf) {
Write-Output "---------------Cached JDK Path File---------"
Write-Host (Get-ChildItem -Path "." -Filter "jdkpath.txt").FullName -ForegroundColor Blue
}
else {
Write-Warning "write cache file failed,contact the author fix sh."
}
}
function Change-JDK {
if (Test-Path -Path "jdkpath.txt" -PathType leaf) {
$check = Read-Host -Prompt "Rebuild cache, default no/yes"
if ($check.toString().ToLower() -eq "yes") {
Out-Cache
}
}
else {
Write-Output "---------------Build JDK Path Cache File For Waiting----------"
Out-Cache
}
$cache = Get-Content -Path .\jdkpath.txt
Write-Output "---------------List Installed Jdk Path----------"
foreach ($file in $cache) {
Write-Host $cache.IndexOf($file)":"$file -ForegroundColor Green
}
Write-Output "---------------List Using JDK Path--------------"
Write-Host $env:JAVA_HOME -ForegroundColor Blue
while ($true) {
$content = Read-Host -Prompt "---------------Expected Order Numbered(exit code: -1)"
if ('' -eq $content.Trim()) {
continue
}
$num = [System.Convert]::ToInt32($content.Trim())
if (-1 -eq $num) {
break
}
if (($num -ge 0) -and ($num -lt $cache.Length)) {
$jdkFolder = $cache.get($num)
Write-Host $jdkFolder -ForegroundColor Yellow
[System.Environment]::SetEnvironmentVariable('JAVA_HOME', $jdkFolder, 'User')
Write-Host "---------------Check In New Window---------------" -ForegroundColor Red
break
}
else {
$len = $cache.Length - 1
Write-Host "---------------0<= number <=$len" -ForegroundColor Yellow
continue
}
}
}