-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJavaInfoTest.ps1
189 lines (169 loc) · 4.95 KB
/
JavaInfoTest.ps1
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Copyright (C) 2020-2023 by Bill Stewart (bstewart at iname.com)
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Lesser Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see https://www.gnu.org/licenses/.
#requires -version 2
# Prerequisites:
# * 64-bit JavaInfo.dll v1.2.0.0 or later in x86_64 directory
# * 32-bit JavaInfo.dll v1.2.0.0 or later in i386 directory
# If you use -MinimumVersion parameter, specify a version number string as
# a[.b[.c[.d]]]; e.g. "-MinimumVersion 8" would test for at least Java 8
param(
[String]
$MinimumVersion
)
function Get-Platform {
if ( [IntPtr]::Size -eq 8 ) {
"x86_64"
}
else {
"i386"
}
}
$APIDefs = @"
[DllImport("{0}\\JavaInfo.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint IsBinary64Bit(
string FileName,
out uint Is64bit
);
[DllImport("{0}\\JavaInfo.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint IsJavaInstalled();
[DllImport("{0}\\JavaInfo.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint IsJavaMinimumVersion(
string Version,
out uint VersionOK
);
[DllImport("{0}\\JavaInfo.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint GetJavaHome(
StringBuilder PathName,
uint NumChars
);
[DllImport("{0}\\JavaInfo.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint GetJavaJVMPath(
StringBuilder PathName,
uint NumChars
);
[DllImport("{0}\\JavaInfo.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint GetJavaVersion(
StringBuilder Version,
uint NumChars
);
"@ -f (Get-Platform)
$JavaInfo = Add-Type -Name JavaInfo `
-MemberDefinition $APIDefs `
-Namespace "D8BE189018594F8F841FDF1359E47C6C" `
-UsingNamespace "System.Text" `
-PassThru `
-ErrorAction Stop
# DLL function wrapper to retrieve strings
function Get-DLLString {
param(
[Management.Automation.PSMethod]
$dllFunction
)
$result = ""
# Create a StringBuilder with 0 capacity to get string length
$stringBuilder = New-Object Text.StringBuilder(0)
# Invoke function with 0 for 2nd parameter to get length as return value
$numChars = $dllFunction.Invoke($stringBuilder,0)
if ( $numChars -gt 0 ) {
# Specify size of string and call function again
$stringBuilder.Capacity = $numChars
if ( $dllFunction.Invoke($stringBuilder,$numChars) -gt 0 ) {
$result = $stringBuilder.ToString()
}
}
$result
}
# Wrapper for GetJavaHome() DLL function
function Get-JavaHome {
Get-DLLString ($JavaInfo::GetJavaHome)
}
function Get-JavaJVMPath {
Get-DLLString ($JavaInfo::GetJavaJVMPath)
}
# Wrapper for GetJavaVersion() DLL function
function Get-JavaVersion {
Get-DLLString ($JavaInfo::GetJavaVersion)
}
# Wrapper for IsBinary64Bit() DLL function
function Get-Binary64Bit {
param(
[String]
$fileName
)
$result = "Unknown"
if ( $fileName ) {
# Variable must exist before calling as [Ref] parameter
$is64Bit = $null
# Function returns 0 if successful
if ( $JavaInfo::IsBinary64Bit($fileName,[Ref] $is64Bit) -eq 0 ) {
if ( $is64Bit -eq 1 ) {
$result = "Yes"
}
else {
$result = "No"
}
}
}
$result
}
# Wrapper for IsJavaMinimumVersion() DLL function
function Get-JavaMinimumVersion {
param(
[String]
$version
)
$result = ""
# Variable must exist before calling as [Ref] parameter
$versionOK = $null
if ( $JavaInfo::IsJavaMinimumVersion($version,[Ref] $versionOK) -eq 0 ) {
if ( $versionOK -eq 1 ) {
$result = "Yes"
}
else {
$result = "No"
}
}
$result
}
# Main body of script follows...
try {
$IsJavaInstalled = $JavaInfo::IsJavaInstalled() -eq 1
}
catch {
# End script with error message if DLL function failed
Write-Error $_
return
}
if ( $IsJavaInstalled ) {
$JavaHome = Get-JavaHome
$JavaJVMPath = Get-JavaJVMPath
$JavaBinary = Join-Path $JavaHome "bin\java.exe"
"Java home:`t{0}" -f $JavaHome
"jvm.dll path:`t{0}" -f $JavaJVMPath
"Java version:`t{0}" -f (Get-JavaVersion)
"Java is 64-bit:`t{0}" -f (Get-Binary64Bit $JavaBinary)
if ( $MinimumVersion ) {
$TestVersion = Get-JavaMinimumVersion $MinimumVersion
if ( $TestVersion -ne "" ) {
"At least version {0}:`t{1}" -f $MinimumVersion,$TestVersion
}
else {
Write-Warning "Invalid version specified with -MinimumVersion parameter."
}
}
}
else {
"JavaInfo.dll did not detect a Java installation."
}