Menu

[19df57]: / head_src / head.c  Maximize  Restore  History

Download this file

820 lines (722 with data), 21.7 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
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/*
Launch4j (http://launch4j.sourceforge.net/)
Cross-platform Java application wrapper for creating Windows native executables.
Copyright (c) 2004, 2014 Grzegorz Kowal,
Ian Roberts (jdk preference patch)
Sylvain Mina (single instance patch)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
Except as contained in this notice, the name(s) of the above copyright holders
shall not be used in advertising or otherwise to promote the sale, use or other
dealings in this Software without prior written authorization.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "resource.h"
#include "head.h"
HMODULE hModule;
FILE* hLog;
BOOL debugAll = FALSE;
BOOL console = FALSE;
BOOL wow64 = FALSE;
BOOL corruptedJreFound = FALSE;
int runtimeBits = INIT_RUNTIME_BITS;
int foundJava = NO_JAVA_FOUND;
struct _stat statBuf;
PROCESS_INFORMATION pi;
DWORD priority;
char mutexName[STR] = {0};
char errUrl[256] = {0};
char errTitle[STR] = "Launch4j";
char errMsg[BIG_STR] = {0};
char javaMinVer[STR] = {0};
char javaMaxVer[STR] = {0};
char foundJavaVer[STR] = {0};
char foundJavaKey[_MAX_PATH] = {0};
char foundJavaHome[_MAX_PATH] = {0};
char oldPwd[_MAX_PATH] = {0};
char workingDir[_MAX_PATH] = {0};
char cmd[_MAX_PATH] = {0};
char args[MAX_ARGS] = {0};
FILE* openLogFile(const char* exePath, const int pathLen) {
char path[_MAX_PATH] = {0};
strncpy(path, exePath, pathLen);
strcat(path, "\\launch4j.log");
return fopen(path, "a");
}
void closeLogFile() {
if (hLog != NULL) {
fclose(hLog);
}
}
void setWow64Flag() {
LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(
GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
if (fnIsWow64Process != NULL) {
fnIsWow64Process(GetCurrentProcess(), &wow64);
}
debug("WOW64:\t\t%s\n", wow64 ? "yes" : "no");
}
void setConsoleFlag() {
console = TRUE;
}
void msgBox(const char* text) {
if (console) {
printf("%s: %s\n", errTitle, text);
} else {
MessageBox(NULL, text, errTitle, MB_OK);
}
}
void signalError() {
DWORD err = GetLastError();
debug("Error msg:\t%s\n", errMsg);
if (err) {
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL);
debug(ERROR_FORMAT, (LPCTSTR) lpMsgBuf);
strcat(errMsg, "\n\n");
strcat(errMsg, (LPCTSTR) lpMsgBuf);
LocalFree(lpMsgBuf);
}
msgBox(errMsg);
if (*errUrl) {
debug("Open URL:\t%s\n", errUrl);
ShellExecute(NULL, "open", errUrl, NULL, NULL, SW_SHOWNORMAL);
}
closeLogFile();
}
BOOL loadString(const int resID, char* buffer) {
HRSRC hResource;
HGLOBAL hResourceLoaded;
LPBYTE lpBuffer;
hResource = FindResourceEx(hModule, RT_RCDATA, MAKEINTRESOURCE(resID),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT));
if (NULL != hResource) {
hResourceLoaded = LoadResource(hModule, hResource);
if (NULL != hResourceLoaded) {
lpBuffer = (LPBYTE) LockResource(hResourceLoaded);
if (NULL != lpBuffer) {
int x = 0;
do {
buffer[x] = (char) lpBuffer[x];
} while (buffer[x++] != 0);
if (debugAll) {
debug("Resource %d:\t%s\n", resID, buffer);
}
return TRUE;
}
}
} else {
SetLastError(0);
buffer[0] = 0;
}
return FALSE;
}
BOOL loadBool(const int resID) {
char boolStr[20] = {0};
loadString(resID, boolStr);
return strcmp(boolStr, TRUE_STR) == 0;
}
int loadInt(const int resID) {
char intStr[20] = {0};
loadString(resID, intStr);
return atoi(intStr);
}
BOOL regQueryValue(const char* regPath, unsigned char* buffer,
unsigned long bufferLength) {
HKEY hRootKey;
char* key;
char* value;
if (strstr(regPath, HKEY_CLASSES_ROOT_STR) == regPath) {
hRootKey = HKEY_CLASSES_ROOT;
} else if (strstr(regPath, HKEY_CURRENT_USER_STR) == regPath) {
hRootKey = HKEY_CURRENT_USER;
} else if (strstr(regPath, HKEY_LOCAL_MACHINE_STR) == regPath) {
hRootKey = HKEY_LOCAL_MACHINE;
} else if (strstr(regPath, HKEY_USERS_STR) == regPath) {
hRootKey = HKEY_USERS;
} else if (strstr(regPath, HKEY_CURRENT_CONFIG_STR) == regPath) {
hRootKey = HKEY_CURRENT_CONFIG;
} else {
return FALSE;
}
key = strchr(regPath, '\\') + 1;
value = strrchr(regPath, '\\') + 1;
*(value - 1) = 0;
HKEY hKey;
unsigned long datatype;
BOOL result = FALSE;
if ((wow64 && RegOpenKeyEx(hRootKey,
key,
0,
KEY_READ | KEY_WOW64_64KEY,
&hKey) == ERROR_SUCCESS)
|| RegOpenKeyEx(hRootKey,
key,
0,
KEY_READ,
&hKey) == ERROR_SUCCESS) {
result = RegQueryValueEx(hKey, value, NULL, &datatype, buffer, &bufferLength)
== ERROR_SUCCESS;
RegCloseKey(hKey);
}
*(value - 1) = '\\';
return result;
}
void regSearch(const HKEY hKey, const char* keyName, const int searchType) {
DWORD x = 0;
unsigned long versionSize = _MAX_PATH;
FILETIME time;
char fullKeyName[_MAX_PATH] = {0};
char version[_MAX_PATH] = {0};
while (RegEnumKeyEx(
hKey, // handle to key to enumerate
x++, // index of subkey to enumerate
version, // address of buffer for subkey name
&versionSize, // address for size of subkey buffer
NULL, // reserved
NULL, // address of buffer for class string
NULL, // address for size of class buffer
&time) == ERROR_SUCCESS) {
strcpy(fullKeyName, keyName);
appendPath(fullKeyName, version);
debug("Check:\t\t%s\n", fullKeyName);
if (strcmp(version, javaMinVer) >= 0
&& (!*javaMaxVer || strcmp(version, javaMaxVer) <= 0)
&& strcmp(version, foundJavaVer) > 0
&& isJavaHomeValid(fullKeyName, searchType)) {
strcpy(foundJavaVer, version);
strcpy(foundJavaKey, fullKeyName);
foundJava = searchType;
debug("Match:\t\t%s\n", version);
} else {
debug("Ignore:\t\t%s\n", version);
}
versionSize = _MAX_PATH;
}
}
BOOL isJavaHomeValid(const char* keyName, const int searchType) {
BOOL valid = FALSE;
HKEY hKey;
char path[_MAX_PATH] = {0};
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
keyName,
0,
KEY_READ | (searchType & KEY_WOW64_64KEY),
&hKey) == ERROR_SUCCESS) {
unsigned char buffer[_MAX_PATH] = {0};
unsigned long bufferlength = _MAX_PATH;
unsigned long datatype;
if (RegQueryValueEx(hKey, "JavaHome", NULL, &datatype, buffer,
&bufferlength) == ERROR_SUCCESS) {
int i = 0;
do {
path[i] = buffer[i];
} while (path[i++] != 0);
if (searchType & FOUND_SDK) {
appendPath(path, "jre");
}
valid = isLauncherPathValid(path);
}
RegCloseKey(hKey);
}
if (valid) {
strcpy(foundJavaHome, path);
} else {
corruptedJreFound = TRUE;
}
return valid;
}
BOOL isLauncherPathValid(const char* path) {
char javaw[_MAX_PATH];
BOOL result = FALSE;
if (*path) {
strcpy(javaw, path);
appendJavaw(javaw);
result = _stat(javaw, &statBuf) == 0;
if (!result) {
// Don't display additional info in the error popup.
SetLastError(0);
}
}
debug("Check launcher:\t%s %s\n", javaw, result ? "(OK)" : "(not found)");
return result;
}
void regSearchWow(const char* keyName, const int searchType) {
HKEY hKey;
if (runtimeBits == INIT_RUNTIME_BITS) {
runtimeBits = loadInt(RUNTIME_BITS);
}
if ((runtimeBits & USE_64_BIT_RUNTIME) && wow64) {
debug("64-bit search:\t%s...\n", keyName);
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
keyName,
0,
KEY_READ | KEY_WOW64_64KEY,
&hKey) == ERROR_SUCCESS) {
regSearch(hKey, keyName, searchType | KEY_WOW64_64KEY);
RegCloseKey(hKey);
if ((foundJava & KEY_WOW64_64KEY) != NO_JAVA_FOUND) {
return;
}
}
}
if (runtimeBits & USE_32_BIT_RUNTIME) {
debug("32-bit search:\t%s...\n", keyName);
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
keyName,
0,
KEY_READ,
&hKey) == ERROR_SUCCESS) {
regSearch(hKey, keyName, searchType);
RegCloseKey(hKey);
}
}
}
void regSearchJreSdk(const char* jreKeyName, const char* sdkKeyName,
const int jdkPreference) {
if (jdkPreference == JDK_ONLY || jdkPreference == PREFER_JDK) {
regSearchWow(sdkKeyName, FOUND_SDK);
if (jdkPreference != JDK_ONLY) {
regSearchWow(jreKeyName, FOUND_JRE);
}
} else { // jdkPreference == JRE_ONLY or PREFER_JRE
regSearchWow(jreKeyName, FOUND_JRE);
if (jdkPreference != JRE_ONLY) {
regSearchWow(sdkKeyName, FOUND_SDK);
}
}
}
BOOL findJavaHome(char* path, const int jdkPreference) {
regSearchJreSdk("SOFTWARE\\JavaSoft\\Java Runtime Environment",
"SOFTWARE\\JavaSoft\\Java Development Kit",
jdkPreference);
if (foundJava == NO_JAVA_FOUND) {
regSearchJreSdk("SOFTWARE\\IBM\\Java2 Runtime Environment",
"SOFTWARE\\IBM\\Java Development Kit",
jdkPreference);
}
if (foundJava != NO_JAVA_FOUND) {
strcpy(path, foundJavaHome);
debug("Runtime used:\t%s (%s-bit)\n", foundJavaVer,
(foundJava & KEY_WOW64_64KEY) != NO_JAVA_FOUND ? "64" : "32");
return TRUE;
}
return FALSE;
}
/*
* Extract the executable name, returns path length.
*/
int getExePath(char* exePath) {
if (GetModuleFileName(hModule, exePath, _MAX_PATH) == 0) {
return -1;
}
return strrchr(exePath, '\\') - exePath;
}
void appendPath(char* basepath, const char* path) {
if (basepath[strlen(basepath) - 1] != '\\') {
strcat(basepath, "\\");
}
strcat(basepath, path);
}
void appendJavaw(char* jrePath) {
if (console) {
appendPath(jrePath, "bin\\java.exe");
} else {
appendPath(jrePath, "bin\\javaw.exe");
}
}
void appendAppClasspath(char* dst, const char* src) {
strcat(dst, src);
strcat(dst, ";");
}
/*
* Expand environment %variables%
*/
BOOL expandVars(char *dst, const char *src, const char *exePath, const int pathLen) {
char varName[STR];
char varValue[MAX_VAR_SIZE];
while (strlen(src) > 0) {
char *start = strchr(src, '%');
if (start != NULL) {
char *end = strchr(start + 1, '%');
if (end == NULL) {
return FALSE;
}
// Copy content up to %VAR%
strncat(dst, src, start - src);
// Insert value of %VAR%
*varName = 0;
strncat(varName, start + 1, end - start - 1);
// Remember value start for logging
char *currentVarValue = dst + strlen(dst);
if (strcmp(varName, "EXEDIR") == 0) {
strncat(dst, exePath, pathLen);
} else if (strcmp(varName, "EXEFILE") == 0) {
strcat(dst, exePath);
} else if (strcmp(varName, "PWD") == 0) {
GetCurrentDirectory(_MAX_PATH, dst + strlen(dst));
} else if (strcmp(varName, "OLDPWD") == 0) {
strcat(dst, oldPwd);
} else if (strstr(varName, HKEY_STR) == varName) {
regQueryValue(varName, dst + strlen(dst), BIG_STR);
} else if (GetEnvironmentVariable(varName, varValue, MAX_VAR_SIZE) > 0) {
strcat(dst, varValue);
}
debug("Substitute:\t%s = %s\n", varName, currentVarValue);
src = end + 1;
} else {
// Copy remaining content
strcat(dst, src);
break;
}
}
return TRUE;
}
void appendHeapSizes(char *dst) {
MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
GlobalMemoryStatusEx(&statex);
appendHeapSize(dst, INITIAL_HEAP_SIZE, INITIAL_HEAP_PERCENT,
statex.ullAvailPhys, "-Xms");
appendHeapSize(dst, MAX_HEAP_SIZE, MAX_HEAP_PERCENT,
statex.ullAvailPhys, "-Xmx");
}
void appendHeapSize(char *dst, const int megabytesID, const int percentID,
const DWORDLONG availableMemory, const char *option) {
const int mb = 1048576; // 1 MB
const int mbLimit32 = 1024; // Max heap size in MB on 32-bit JREs
const int megabytes = loadInt(megabytesID);
const int percent = loadInt(percentID);
const int availableMb = availableMemory * percent / (100 * mb); // 100% * 1 MB
int heapSizeMb = availableMb > megabytes ? availableMb : megabytes;
if (heapSizeMb > 0) {
if (!(foundJava & KEY_WOW64_64KEY) && heapSizeMb > mbLimit32) {
debug("Heap limit:\tReduced %d MB heap size to 32-bit maximum %d MB\n",
heapSizeMb, mbLimit32);
heapSizeMb = mbLimit32;
}
debug("Heap %s:\tRequested %d MB / %d%%, Available: %d MB, Heap size: %d MB\n",
option, megabytes, percent, (int)(availableMemory / mb), heapSizeMb);
strcat(dst, option);
_itoa(heapSizeMb, dst + strlen(dst), 10); // 10 -- radix
strcat(dst, "m ");
}
}
void setJvmOptions(char *jvmOptions, const char *exePath) {
if (loadString(JVM_OPTIONS, jvmOptions)) {
strcat(jvmOptions, " ");
}
/*
* Load additional JVM options from .l4j.ini file
* Options are separated by spaces or CRLF
* # starts an inline comment
*/
char iniFilePath[_MAX_PATH] = {0};
strncpy(iniFilePath, exePath, strlen(exePath) - 3);
strcat(iniFilePath, "l4j.ini");
long hFile;
if ((hFile = _open(iniFilePath, _O_RDONLY)) != -1) {
debug("Loading:\t%s\n", iniFilePath);
const int jvmOptLen = strlen(jvmOptions);
char* src = jvmOptions + jvmOptLen;
char* dst = src;
const int len = _read(hFile, src, MAX_ARGS - jvmOptLen - BIG_STR);
BOOL copy = TRUE;
int i;
for (i = 0; i < len; i++, src++) {
if (*src == '#') {
copy = FALSE;
} else if (*src == 13 || *src == 10) {
copy = TRUE;
if (dst > jvmOptions && *(dst - 1) != ' ') {
*dst++ = ' ';
}
} else if (copy) {
*dst++ = *src;
}
}
*dst = 0;
if (len > 0 && *(dst - 1) != ' ') {
strcat(jvmOptions, " ");
}
_close(hFile);
}
}
int prepare(const char *lpCmdLine) {
char tmp[MAX_ARGS] = {0};
hModule = GetModuleHandle(NULL);
if (hModule == NULL) {
return FALSE;
}
// Get executable path
char exePath[_MAX_PATH] = {0};
int pathLen = getExePath(exePath);
if (pathLen == -1) {
return FALSE;
}
// Initialize logging
if (strstr(lpCmdLine, "--l4j-debug") != NULL) {
hLog = openLogFile(exePath, pathLen);
if (hLog == NULL) {
return FALSE;
}
debugAll = strstr(lpCmdLine, "--l4j-debug-all") != NULL;
debug("\n\nVersion:\t%s\n", VERSION);
debug("CmdLine:\t%s %s\n", exePath, lpCmdLine);
}
setWow64Flag();
// Set default error message, title and optional support web site url.
loadString(SUPPORT_URL, errUrl);
loadString(ERR_TITLE, errTitle);
if (!loadString(STARTUP_ERR, errMsg)) {
debug(ERROR_FORMAT, "Startup error message not defined.");
return FALSE;
}
// Single instance
loadString(MUTEX_NAME, mutexName);
if (*mutexName) {
SECURITY_ATTRIBUTES security;
security.nLength = sizeof(SECURITY_ATTRIBUTES);
security.bInheritHandle = TRUE;
security.lpSecurityDescriptor = NULL;
CreateMutexA(&security, FALSE, mutexName);
if (GetLastError() == ERROR_ALREADY_EXISTS) {
debug(ERROR_FORMAT, "Instance already exists.");
return ERROR_ALREADY_EXISTS;
}
}
// Working dir
char tmp_path[_MAX_PATH] = {0};
GetCurrentDirectory(_MAX_PATH, oldPwd);
if (loadString(CHDIR, tmp_path)) {
strncpy(workingDir, exePath, pathLen);
appendPath(workingDir, tmp_path);
_chdir(workingDir);
debug("Working dir:\t%s\n", workingDir);
}
// Use bundled jre or find java
if (loadString(JRE_PATH, tmp_path)) {
char jrePath[MAX_ARGS] = {0};
expandVars(jrePath, tmp_path, exePath, pathLen);
debug("Bundled JRE:\t%s\n", jrePath);
if (jrePath[0] == '\\' || jrePath[1] == ':') {
// Absolute
strcpy(cmd, jrePath);
} else {
// Relative
strncpy(cmd, exePath, pathLen);
appendPath(cmd, jrePath);
}
if (isLauncherPathValid(cmd)) {
foundJava = (wow64 && loadBool(BUNDLED_JRE_64_BIT))
? FOUND_BUNDLED | KEY_WOW64_64KEY
: FOUND_BUNDLED;
}
}
if (foundJava == NO_JAVA_FOUND) {
if (!loadString(JAVA_MIN_VER, javaMinVer)) {
loadString(BUNDLED_JRE_ERR, errMsg);
return FALSE;
}
loadString(JAVA_MAX_VER, javaMaxVer);
if (!findJavaHome(cmd, loadInt(JDK_PREFERENCE))) {
loadString(JRE_VERSION_ERR, errMsg);
strcat(errMsg, " ");
strcat(errMsg, javaMinVer);
if (*javaMaxVer) {
strcat(errMsg, " - ");
strcat(errMsg, javaMaxVer);
}
if (runtimeBits == USE_64_BIT_RUNTIME
|| runtimeBits == USE_32_BIT_RUNTIME) {
strcat(errMsg, " (");
strcat(errMsg, runtimeBits == USE_64_BIT_RUNTIME ? "64" : "32");
strcat(errMsg, "-bit)");
}
if (corruptedJreFound) {
char launcherErrMsg[BIG_STR] = {0};
if (loadString(LAUNCHER_ERR, launcherErrMsg)) {
strcat(errMsg, "\n");
strcat(errMsg, launcherErrMsg);
}
}
loadString(DOWNLOAD_URL, errUrl);
return FALSE;
}
}
// Append a path to the Path environment variable
char jreBinPath[_MAX_PATH];
strcpy(jreBinPath, cmd);
strcat(jreBinPath, "\\bin");
if (!appendToPathVar(jreBinPath)) {
debug(ERROR_FORMAT, "appendToPathVar failed.");
return FALSE;
}
// Set environment variables
char envVars[MAX_VAR_SIZE] = {0};
loadString(ENV_VARIABLES, envVars);
char *var = strtok(envVars, "\t");
while (var != NULL) {
char *varValue = strchr(var, '=');
*varValue++ = 0;
*tmp = 0;
expandVars(tmp, varValue, exePath, pathLen);
debug("Set var:\t%s = %s\n", var, tmp);
SetEnvironmentVariable(var, tmp);
var = strtok(NULL, "\t");
}
*tmp = 0;
// Process priority
priority = loadInt(PRIORITY_CLASS);
// Launcher
appendJavaw(cmd);
// Heap sizes
appendHeapSizes(args);
// JVM options
char jvmOptions[MAX_ARGS] = {0};
setJvmOptions(jvmOptions, exePath);
// Expand environment %variables%
expandVars(args, jvmOptions, exePath, pathLen);
// MainClass + Classpath or Jar
char mainClass[STR] = {0};
char jar[_MAX_PATH] = {0};
const BOOL wrapper = loadBool(WRAPPER);
loadString(JAR, jar);
if (loadString(MAIN_CLASS, mainClass)) {
if (!loadString(CLASSPATH, tmp)) {
debug("Info:\t\tClasspath not defined.\n");
}
char exp[MAX_ARGS] = {0};
expandVars(exp, tmp, exePath, pathLen);
strcat(args, "-classpath \"");
if (wrapper) {
appendAppClasspath(args, exePath);
} else if (*jar) {
appendAppClasspath(args, jar);
}
// Deal with wildcards or >> strcat(args, exp); <<
char* cp = strtok(exp, ";");
while(cp != NULL) {
debug("Add classpath:\t%s\n", cp);
if (strpbrk(cp, "*?") != NULL) {
int len = strrchr(cp, '\\') - cp + 1;
strncpy(tmp_path, cp, len);
char* filename = tmp_path + len;
*filename = 0;
struct _finddata_t c_file;
long hFile;
if ((hFile = _findfirst(cp, &c_file)) != -1L) {
do {
strcpy(filename, c_file.name);
appendAppClasspath(args, tmp_path);
debug(" \" :\t%s\n", tmp_path);
} while (_findnext(hFile, &c_file) == 0);
}
_findclose(hFile);
} else {
appendAppClasspath(args, cp);
}
cp = strtok(NULL, ";");
}
*(args + strlen(args) - 1) = 0;
strcat(args, "\" ");
strcat(args, mainClass);
} else if (wrapper) {
strcat(args, "-jar \"");
strcat(args, exePath);
strcat(args, "\"");
} else {
strcat(args, "-jar \"");
strncat(args, exePath, pathLen);
appendPath(args, jar);
strcat(args, "\"");
}
// Constant command line args
if (loadString(CMD_LINE, tmp)) {
strcat(args, " ");
strcat(args, tmp);
}
// Command line args
if (*lpCmdLine) {
strcpy(tmp, lpCmdLine);
char* dst;
while ((dst = strstr(tmp, "--l4j-")) != NULL) {
char* src = strchr(dst, ' ');
if (src == NULL || *(src + 1) == 0) {
*dst = 0;
} else {
strcpy(dst, src + 1);
}
}
if (*tmp) {
strcat(args, " ");
strcat(args, tmp);
}
}
debug("Launcher:\t%s\n", cmd);
debug("Launcher args:\t%s\n", args);
debug("Args length:\t%d/32768 chars\n", strlen(args));
return TRUE;
}
void closeHandles() {
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
closeLogFile();
}
/*
* Append a path to the Path environment variable
*/
BOOL appendToPathVar(const char* path) {
char chBuf[MAX_VAR_SIZE] = {0};
const int pathSize = GetEnvironmentVariable("Path", chBuf, MAX_VAR_SIZE);
if (MAX_VAR_SIZE - pathSize - 1 < strlen(path)) {
return FALSE;
}
strcat(chBuf, ";");
strcat(chBuf, path);
return SetEnvironmentVariable("Path", chBuf);
}
DWORD execute(const BOOL wait) {
STARTUPINFO si;
memset(&pi, 0, sizeof(pi));
memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
DWORD dwExitCode = -1;
char cmdline[MAX_ARGS];
strcpy(cmdline, "\"");
strcat(cmdline, cmd);
strcat(cmdline, "\" ");
strcat(cmdline, args);
if (CreateProcess(NULL, cmdline, NULL, NULL,
TRUE, priority, NULL, NULL, &si, &pi)) {
if (wait) {
WaitForSingleObject(pi.hProcess, INFINITE);
GetExitCodeProcess(pi.hProcess, &dwExitCode);
debug("Exit code:\t%d\n", dwExitCode);
closeHandles();
} else {
dwExitCode = 0;
}
}
return dwExitCode;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.