-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwsJavaInfo.pp
558 lines (521 loc) · 17 KB
/
wsJavaInfo.pp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
{ 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/.
}
{$MODE OBJFPC}
{$MODESWITCH UNICODESTRINGS}
unit wsJavaInfo;
interface
type
TJavaDetectionType = (JDNone, JDEnvironment, JDPath, JDJavaSoft, JDIBM,
JDAdoptium, JDMicrosoft, JDZulu);
// Gets whether specified binary is 64-bit or not in Is64Bit parameter; returns
// 0 for success, or non-zero for failure
function wsIsBinary64Bit(FileName: string; var Is64Bit: Boolean): DWORD;
// If Java installation found, returns path of Java home directory
function wsGetJavaHome(): string;
// If Java installation found, returns path for jvm.dll
function wsGetJavaJVMPath(): string;
// If Java installation found, returns version number of java.exe as a string
// (a.b.c.d)
function wsGetJavaVersion(): string;
// Returns true if a Java installation was found, or false otherwise
function wsIsJavaInstalled(): Boolean;
// Gets whether the installed Java version is at least the specified version
// in the VersionOK parameter; returns true for success, or false otherwise
function wsIsJavaMinimumVersion(Version: string; var VersionOK: Boolean): Boolean;
// Diagnostic: returns Java detection type
function wsGetJavaDetectionType(): TJavaDetectionType;
implementation
uses
Windows,
wsUtilArch,
wsUtilEnv,
wsUtilFile,
wsUtilReg,
wsUtilStr,
wsUtilVer;
type
TStringArray = array of string;
var
JavaHome, JavaJVMPath, JavaVersion: string;
JavaDetectionType: TJavaDetectionType;
function wsIsBinary64Bit(FileName: string; var Is64Bit: Boolean): DWORD;
var
BinaryType: Word;
begin
result := GetBinaryType(FileName, BinaryType);
if result = 0 then
Is64Bit := BinaryType <> IMAGE_FILE_MACHINE_I386;
end;
// Find Java home from environment variables
function FindJavaHomeEnvironment(): string;
var
VarList: TStringArray;
I: Integer;
begin
result := '';
SetLength(VarList, 3);
VarList[0] := 'JAVA_HOME';
VarList[1] := 'JDK_HOME';
VarList[2] := 'JRE_HOME';
for I := 0 to Length(VarList) - 1 do
begin
result := GetEnvVar(VarList[I]);
if result <> '' then
break;
end;
end;
// Find Java home by searching the Path
function FindJavaHomePath(): string;
begin
result := FileSearch('java.exe', GetEnvVar('Path'));
if result <> '' then
result := ExtractFileDir(ExtractFileDir(result));
end;
// Find latest Java home using JavaSoft-style registry subkeys
// Subkey: SOFTWARE\<vendor>\<javatype>\<version>
// String value: 'JavaHome'
// Where
// <vendor> is usually 'JavaSoft' or 'IBM'
// <javatype> is JDK or JRE (or spelled out versions of those)
// <version> is the Java version string
// StartingSubKey is subkey where to start search (e.g., 'SOFTWARE\JavaSoft')
// If starting subkey is 'SOFTWARE\JavaSoft', this function should detect:
// * Oracle JDK/JRE
// * Adoptium if 'JavaSoft (Oracle) registry keys' component selected
// * Amazon Corretto JDK if 'Setup Registry Keys' component selected
// * Azul Systems Zulu JDK
// If starting subkey is 'SOFTWARE\IBM', this function should detect IBM JDK
// (hopefully? I have no way to test, as I don't have a way to get an IBM
// JRE/JDK on Windows)
function FindJavaHomeRegistryJavaSoft(const StartingSubKey: string): string;
var
LatestSubKeyName, LatestVersion, ResultStr: string;
RootKey: HKEY;
I, J: Integer;
StartingSubKeys, SubKeyNames: TStringArray;
SubKeyExists: Boolean;
begin
result := '';
LatestSubKeyName := '';
LatestVersion := '0';
RootKey := 0;
SetLength(StartingSubKeys, 4);
StartingSubKeys[0] := JoinPath(StartingSubKey, 'Java Development Kit');
StartingSubKeys[1] := JoinPath(StartingSubKey, 'JDK');
StartingSubKeys[2] := JoinPath(StartingSubKey, 'Java Runtime Environment');
StartingSubKeys[3] := JoinPath(StartingSubKey, 'JRE');
for I := 0 to Length(StartingSubKeys) - 1 do
begin
SubKeyExists := false;
if IsWin64() then
begin
SubKeyExists := RegKeyExists(HKEY_LOCAL_MACHINE_64, StartingSubKeys[I]);
if SubKeyExists then
RootKey := HKEY_LOCAL_MACHINE_64
else
begin
SubKeyExists := RegKeyExists(HKEY_LOCAL_MACHINE_32, StartingSubKeys[I]);
if SubKeyExists then
RootKey := HKEY_LOCAL_MACHINE_32;
end;
end
else
begin
SubKeyExists := RegKeyExists(HKEY_LOCAL_MACHINE, StartingSubKeys[I]);
if SubKeyExists then
RootKey := HKEY_LOCAL_MACHINE;
end;
if SubKeyExists then
begin
if RegGetSubKeyNames(RootKey, StartingSubKeys[I], SubKeyNames) and (Length(SubKeyNames) > 0) then
begin
for J := 0 to Length(SubKeyNames) - 1 do
begin
if CompareVersionStrings(SubKeyNames[J], LatestVersion) > 0 then
begin
LatestSubKeyName := StartingSubKeys[I];
LatestVersion := SubKeyNames[J];
end;
end;
end;
end;
end;
if (LatestVersion <> '0') and (RegQueryStringValue(RootKey, LatestSubKeyName + '\' + LatestVersion,
'JavaHome', ResultStr)) and (ResultStr <> '') then
result := ResultStr;
end;
// Find latest Java home using Adoptium-style registry subkeys
// Subkey: SOFTWARE\<vendor>\<javatype>\<version>\<buildtype>\MSI
// String value: Path
// Where
// <vendor> is one of: 'AdoptOpenJDK', 'Eclipse Adoptium', 'Eclipse Foundation', or 'Semeru'
// <javatype> is JDK or JRE
// <version> is the Java version string
// <buildtype> is usually 'hotspot' or 'openj9'
function FindJavaHomeRegistryAdoptium(): string;
var
LatestSubKeyName, LatestVersion, ResultStr: string;
RootKey: HKEY;
I, J, K: Integer;
StartingSubKeys, SubKeyNames, SubSubKeyNames: TStringArray;
SubKeyExists: Boolean;
begin
result := '';
LatestSubKeyName := '';
LatestVersion := '0';
RootKey := 0;
SetLength(StartingSubKeys, 8);
StartingSubKeys[0] := 'SOFTWARE\AdoptOpenJDK\JDK';
StartingSubKeys[1] := 'SOFTWARE\AdoptOpenJDK\JRE';
StartingSubKeys[2] := 'SOFTWARE\Eclipse Adoptium\JDK';
StartingSubKeys[3] := 'SOFTWARE\Eclipse Adoptium\JRE';
StartingSubKeys[4] := 'SOFTWARE\Eclipse Foundation\JDK';
StartingSubKeys[5] := 'SOFTWARE\Eclipse Foundation\JRE';
StartingSubKeys[6] := 'SOFTWARE\Semeru\JDK';
StartingSubKeys[7] := 'SOFTWARE\Semeru\JRE';
for I := 0 to Length(StartingSubKeys) - 1 do
begin
SubKeyExists := false;
if IsWin64() then
begin
SubKeyExists := RegKeyExists(HKEY_LOCAL_MACHINE_64, StartingSubKeys[I]);
if SubKeyExists then
RootKey := HKEY_LOCAL_MACHINE_64
else
begin
SubKeyExists := RegKeyExists(HKEY_LOCAL_MACHINE_32, StartingSubKeys[I]);
if SubKeyExists then
RootKey := HKEY_LOCAL_MACHINE_32;
end;
end
else
begin
SubKeyExists := RegKeyExists(HKEY_LOCAL_MACHINE, StartingSubKeys[I]);
if SubKeyExists then
RootKey := HKEY_LOCAL_MACHINE;
end;
if SubKeyExists then
begin
if RegGetSubKeyNames(RootKey, StartingSubKeys[I], SubKeyNames) and (Length(SubKeyNames) > 0) then
begin
for J := 0 to Length(SubKeyNames) - 1 do
begin
if CompareVersionStrings(SubKeyNames[J], LatestVersion) > 0 then
begin
if RegGetSubKeyNames(RootKey, StartingSubKeys[I] + '\' + SubKeyNames[J], SubSubKeyNames) and
(Length(SubSubKeyNames) > 0) then
begin
for K := 0 to Length(SubSubKeyNames) - 1 do
begin
LatestSubKeyName := StartingSubKeys[I] + '\' + SubKeyNames[J] + '\' + SubSubKeyNames[K] + '\MSI';
LatestVersion := SubKeyNames[J];
end;
end;
end;
end;
end;
end;
end;
if (LatestVersion <> '0') and (RegQueryStringValue(RootKey, LatestSubKeyName, 'Path', ResultStr)) and (ResultStr <> '') then
result := ResultStr;
end;
// Find latest Java home using Microsoft registry subkeys
// Subkey: SOFTWARE\Microsoft\<javatype>\<version>\<buildtype>\MSI
// String value: Path
// Where
// <javatype> is JDK
// <version> is the Java version string
// <buildtype> is 'hotspot'
function FindJavaHomeRegistryMicrosoft(): string;
var
LatestSubKeyName, LatestVersion, ResultStr: string;
RootKey: HKEY;
I, J, K: Integer;
StartingSubKeys, SubKeyNames, SubSubKeyNames: TStringArray;
SubKeyExists: Boolean;
begin
result := '';
LatestSubKeyName := '';
LatestVersion := '0';
RootKey := 0;
SetLength(StartingSubKeys, 1);
StartingSubKeys[0] := 'SOFTWARE\Microsoft\JDK';
for I := 0 to Length(StartingSubKeys) - 1 do
begin
SubKeyExists := false;
if IsWin64() then
begin
SubKeyExists := RegKeyExists(HKEY_LOCAL_MACHINE_64, StartingSubKeys[I]);
if SubKeyExists then
RootKey := HKEY_LOCAL_MACHINE_64
else
begin
SubKeyExists := RegKeyExists(HKEY_LOCAL_MACHINE_32, StartingSubKeys[I]);
if SubKeyExists then
RootKey := HKEY_LOCAL_MACHINE_32;
end;
end
else
begin
SubKeyExists := RegKeyExists(HKEY_LOCAL_MACHINE, StartingSubKeys[I]);
if SubKeyExists then
RootKey := HKEY_LOCAL_MACHINE;
end;
if SubKeyExists then
begin
if RegGetSubKeyNames(RootKey, StartingSubKeys[I], SubKeyNames) and (Length(SubKeyNames) > 0) then
begin
for J := 0 to Length(SubKeyNames) - 1 do
begin
if CompareVersionStrings(SubKeyNames[J], LatestVersion) > 0 then
begin
if RegGetSubKeyNames(RootKey, StartingSubKeys[I] + '\' + SubKeyNames[J], SubSubKeyNames) and
(Length(SubSubKeyNames) > 0) then
begin
for K := 0 to Length(SubSubKeyNames) - 1 do
begin
LatestSubKeyName := StartingSubKeys[I] + '\' + SubKeyNames[J] + '\' + SubSubKeyNames[K] + '\MSI';
LatestVersion := SubKeyNames[J];
end;
end;
end;
end;
end;
end;
end;
if (LatestVersion <> '0') and (RegQueryStringValue(RootKey, LatestSubKeyName, 'Path', ResultStr)) and (ResultStr <> '') then
result := ResultStr;
end;
// Find latest Java home using Zulu-style registry subkeys
// Subkey: SOFTWARE\Azul Systems\Zulu\zulu-<version>
// String value: InstallationPath
// <version> is the Java version
// (This search would only occur if the Zulu installer doesn't update the
// JavaSoft registry subkeys for some reason)
function FindJavaHomeRegistryZulu(): string;
var
LatestSubKeyName, LatestVersion, SubKeyName, CurrentVersion, ResultStr: string;
RootKey: HKEY;
SubKeyExists: Boolean;
SubKeyNames: TStringArray;
I: Integer;
begin
result := '';
LatestSubKeyName := '';
LatestVersion := '0';
RootKey := 0;
SubKeyExists := false;
SubKeyName := 'SOFTWARE\Azul Systems\Zulu';
if IsWin64() then
begin
SubKeyExists := RegKeyExists(HKEY_LOCAL_MACHINE_64, SubKeyName);
if SubKeyExists then
RootKey := HKEY_LOCAL_MACHINE_64
else
begin
SubKeyExists := RegKeyExists(HKEY_LOCAL_MACHINE_32, SubKeyName);
if SubKeyExists then
RootKey := HKEY_LOCAL_MACHINE_32;
end;
end
else
begin
SubKeyExists := RegKeyExists(HKEY_LOCAL_MACHINE, SubKeyName);
if SubKeyExists then
RootKey := HKEY_LOCAL_MACHINE;
end;
if SubKeyExists then
begin
if RegGetSubKeyNames(RootKey, SubKeyName, SubKeyNames) and (Length(SubKeyNames) > 0) then
begin
for I := 0 to Length(SubKeyNames) - 1 do
begin
if (Length(SubKeyNames[I]) > 5) and (LowerCase(Copy(SubKeyNames[I], 1, 5)) = 'zulu-') then
begin
if GetDigitsInString(SubKeyNames[I], CurrentVersion) then
begin
if CompareVersionStrings(CurrentVersion, LatestVersion) > 0 then
begin
LatestSubKeyName := SubKeyName + '\' + SubKeyNames[I];
LatestVersion := CurrentVersion;
end;
end;
end;
end;
end;
end;
if (LatestVersion <> '0') and (RegQueryStringValue(RootKey, LatestSubKeyName, 'InstallationPath', ResultStr)) and
(ResultStr <> '') then
result := ResultStr;
end;
function GetJavaVersion(const Home: string): string;
var
Binary: string;
begin
result := '';
if (Home <> '') and DirExists(Home) then
begin
Binary := JoinPath(Home, '\bin\java.exe');
if FileExists(Binary) then
result := GetFileVersion(Binary);
end;
end;
function GetJVMPath(const Home: string): string;
var
Binary: string;
begin
result := '';
if (Home <> '') and DirExists(Home) then
begin
Binary := JoinPath(Home, 'bin\server\jvm.dll');
if FileExists(Binary) then
exit(Binary);
Binary := JoinPath(Home, 'jre\bin\server\jvm.dll');
if FileExists(Binary) then
exit(Binary);
end;
end;
procedure GetJavaDetail(var JavaHome, JavaJVMPath, JavaVersion: string;
var JavaDetectionType: TJavaDetectionType);
var
CurrentHome, CurrentVersion, LatestVersion, LatestHome: string;
LatestDetectionType: TJavaDetectionType;
begin
// Initialize
JavaHome := '';
JavaJVMPath := '';
JavaVersion := '';
JavaDetectionType := JDNone;
// Environment variable search: Exit if found
CurrentHome := FindJavaHomeEnvironment();
CurrentVersion := GetJavaVersion(CurrentHome);
if CurrentVersion <> '' then
begin
JavaHome := CurrentHome;
JavaVersion := CurrentVersion;
JavaJVMPath := GetJVMPath(JavaHome);
JavaDetectionType := JDEnvironment;
exit();
end;
// Path search: Exit if found
CurrentHome := FindJavaHomePath();
CurrentVersion := GetJavaVersion(CurrentHome);
if CurrentVersion <> '' then
begin
JavaHome := CurrentHome;
JavaVersion := CurrentVersion;
JavaJVMPath := GetJVMPath(JavaHome);
JavaDetectionType := JDPath;
exit();
end;
// Registry search should return latest version from all searches
LatestHome := '';
LatestVersion := '0';
LatestDetectionType := JDNone;
// Search 'HKLM\SOFTWARE\JavaSoft'
CurrentHome := FindJavaHomeRegistryJavaSoft('SOFTWARE\JavaSoft');
CurrentVersion := GetJavaVersion(CurrentHome);
if CurrentVersion <> '' then
begin
if CompareVersionStrings(CurrentVersion, LatestVersion) > 0 then
begin
LatestHome := CurrentHome;
LatestVersion := CurrentVersion;
LatestDetectionType := JDJavaSoft;
end;
end;
// Search 'HKLM\SOFTWARE\IBM'
CurrentHome := FindJavaHomeRegistryJavaSoft('SOFTWARE\IBM');
CurrentVersion := GetJavaVersion(CurrentHome);
if CurrentVersion <> '' then
begin
if CompareVersionStrings(CurrentVersion, LatestVersion) > 0 then
begin
LatestHome := CurrentHome;
LatestVersion := CurrentVersion;
LatestDetectionType := JDIBM;
end;
end;
// Search Eclipse Adoptium subkeys
CurrentHome := FindJavaHomeRegistryAdoptium();
CurrentVersion := GetJavaVersion(CurrentHome);
if CurrentVersion <> '' then
begin
if CompareVersionStrings(CurrentVersion, LatestVersion) > 0 then
begin
LatestHome := CurrentHome;
LatestVersion := CurrentVersion;
LatestDetectionType := JDAdoptium;
end;
end;
// Search Microsoft subkeys
CurrentHome := FindJavaHomeRegistryMicrosoft();
CurrentVersion := GetJavaVersion(CurrentHome);
if CurrentVersion <> '' then
begin
if CompareVersionStrings(CurrentVersion, LatestVersion) > 0 then
begin
LatestHome := CurrentHome;
LatestVersion := CurrentVersion;
LatestDetectionType := JDMicrosoft;
end;
end;
// Search Zulu subkeys
CurrentHome := FindJavaHomeRegistryZulu();
CurrentVersion := GetJavaVersion(CurrentHome);
if CurrentVersion <> '' then
begin
if CompareVersionStrings(CurrentVersion, LatestVersion) > 0 then
begin
LatestHome := CurrentHome;
LatestVersion := CurrentVersion;
LatestDetectionType := JDZulu;
end;
end;
if (LatestHome <> '') and (LatestVersion <> '0') then
begin
JavaHome := LatestHome;
JavaJVMPath := GetJVMPath(JavaHome);
JavaVersion := LatestVersion;
JavaDetectionType := LatestDetectionType;
end;
end;
function wsGetJavaHome(): string;
begin
result := RemoveBackslashUnlessRoot(JavaHome);
end;
function wsGetJavaJVMPath(): string;
begin
result := JavaJVMPath;
end;
function wsGetJavaVersion(): string;
begin
result := JavaVersion;
end;
function wsIsJavaInstalled(): Boolean;
begin
result := (JavaHome <> '') and (JavaVersion <> '');
end;
function wsIsJavaMinimumVersion(Version: string; var VersionOK: Boolean): Boolean;
begin
result := TestVersionString(Version);
if result then
VersionOK := CompareVersionStrings(JavaVersion, Version) >= 0;
end;
function wsGetJavaDetectionType(): TJavaDetectionType;
begin
result := JavaDetectionType;
end;
initialization
GetJavaDetail(JavaHome, JavaJVMPath, JavaVersion, JavaDetectionType);
end.