Repository navigation
Expand file tree
/
Copy pathResolve-Java.ps1
More file actions
106 lines (98 loc) · 4.42 KB
/
Copy pathResolve-Java.ps1
File metadata and controls
106 lines (98 loc) · 4.42 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
<#
.SYNOPSIS
Finds a JDK new enough to run the Morphe desktop CLI.
.DESCRIPTION
The CLI is built for a newer JDK than the java that is usually first on PATH here, and it
says so through an UnsupportedClassVersionError on a class nobody recognises. Every script
that shells out to it shares this, so they all fail the same readable way.
Candidates are tried in order and each one is asked its version, so a JAVA_HOME pinned to an
old JDK for some other build cannot shadow a new enough java already on PATH.
#>
function Get-JavaMajorVersion {
<# Returns the major version of a java, or 0 if it will not run. #>
param([string]$Java)
$previousErrorActionPreference = $ErrorActionPreference
try {
# Windows PowerShell promotes a native program's stderr to an ErrorRecord under Stop.
# java -version writes its ordinary version banner to stderr, so probe it under Continue
# and merge that stream into the captured output.
$ErrorActionPreference = 'Continue'
$global:LASTEXITCODE = 0
$output = & $Java '-version' 2>&1
$exitCode = $LASTEXITCODE
} catch {
return 0
} finally {
$ErrorActionPreference = $previousErrorActionPreference
}
if ($exitCode -ne 0) { return 0 }
foreach ($line in @($output)) {
# Both shapes: `openjdk version "21.0.12"` and the older `java version "1.8.0_401"`.
if ("$line" -match 'version "1\.([0-9]+)') { return [int]$Matches[1] }
if ("$line" -match 'version "([0-9]+)') { return [int]$Matches[1] }
}
return 0
}
function Resolve-Java {
param([string]$Explicit, [int]$Minimum = 21)
$candidates = [System.Collections.Generic.List[string]]::new()
$preferredCandidates = @(
[pscustomobject]@{ Path = $Explicit; Explicit = $true }
[pscustomobject]@{ Path = $env:HUSHTUMBLR_JAVA; Explicit = $false }
)
foreach ($entry in $preferredCandidates) {
$candidate = $entry.Path
if (-not $candidate) { continue }
# A JDK directory is what the error message asks for, so take it as one when it is one.
if (Test-Path -LiteralPath $candidate -PathType Container) {
$found = $false
foreach ($leaf in @('bin/java.exe', 'bin/java')) {
$inside = Join-Path $candidate $leaf
if (Test-Path -LiteralPath $inside -PathType Leaf) {
$candidates.Add($inside)
$found = $true
break
}
}
if (-not $found) {
if ($entry.Explicit) {
throw "The Java directory passed with -Java contains no bin/java.exe or bin/java: $candidate"
}
# Preserve environment fallback behavior, but retain the rejected location in
# the final diagnostic when no later candidate works.
$candidates.Add((Join-Path $candidate 'bin/java.exe'))
}
continue
}
$candidates.Add($candidate)
}
# Ahead of JAVA_HOME on purpose: a JAVA_HOME held at an old JDK for another build must not
# turn a machine whose PATH java is new enough into a failing one.
$candidates.Add('java')
foreach ($root in @($env:JAVA_HOME, $env:JDK_HOME)) {
if (-not $root) { continue }
foreach ($leaf in @('bin/java.exe', 'bin/java')) {
$path = Join-Path $root $leaf
if (Test-Path -LiteralPath $path -PathType Leaf) {
$candidates.Add($path)
break
}
}
}
$seen = @{}
$rejected = @()
foreach ($candidate in $candidates) {
if ($seen.ContainsKey($candidate)) { continue }
$seen[$candidate] = $true
$version = Get-JavaMajorVersion -Java $candidate
if ($version -ge $Minimum) {
# Callers also locate javac beside Java. Return the executable actually selected
# by PATH rather than the command name, and keep the first matching application.
return (Get-Command -Name $candidate -CommandType Application -ErrorAction Stop |
Select-Object -First 1).Path
}
$rejected += if ($version -gt 0) { "$candidate is Java $version" } else { "$candidate would not run" }
}
throw ("No Java $Minimum or newer was found, and the Morphe desktop CLI needs one. Set " +
'HUSHTUMBLR_JAVA to a JDK, or pass -Java. Tried: ' + ($rejected -join '; ') + '.')
}