-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
java.c
2421 lines (2186 loc) · 77.1 KB
/
java.c
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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* Shared source for 'java' command line tool.
*
* If JAVA_ARGS is defined, then acts as a launcher for applications. For
* instance, the JDK command line tools such as javac and javadoc (see
* makefiles for more details) are built with this program. Any arguments
* prefixed with '-J' will be passed directly to the 'java' command.
*/
/*
* One job of the launcher is to remove command line options which the
* vm does not understand and will not process. These options include
* options which select which style of vm is run (e.g. -client and
* -server) as well as options which select the data model to use.
* Additionally, for tools which invoke an underlying vm "-J-foo"
* options are turned into "-foo" options to the vm. This option
* filtering is handled in a number of places in the launcher, some of
* it in machine-dependent code. In this file, the function
* CheckJvmType removes vm style options and TranslateApplicationArgs
* removes "-J" prefixes. The CreateExecutionEnvironment function processes
* and removes -d<n> options. On unix, there is a possibility that the running
* data model may not match to the desired data model, in this case an exec is
* required to start the desired model. If the data models match, then
* ParseArguments will remove the -d<n> flags. If the data models do not match
* the CreateExecutionEnviroment will remove the -d<n> flags.
*/
#include <assert.h>
#include "java.h"
#include "jni.h"
/*
* A NOTE TO DEVELOPERS: For performance reasons it is important that
* the program image remain relatively small until after SelectVersion
* CreateExecutionEnvironment have finished their possibly recursive
* processing. Watch everything, but resist all temptations to use Java
* interfaces.
*/
#define USE_STDERR JNI_TRUE /* we usually print to stderr */
#define USE_STDOUT JNI_FALSE
static jboolean printVersion = JNI_FALSE; /* print and exit */
static jboolean showVersion = JNI_FALSE; /* print but continue */
static jboolean printUsage = JNI_FALSE; /* print and exit*/
static jboolean printTo = USE_STDERR; /* where to print version/usage */
static jboolean printXUsage = JNI_FALSE; /* print and exit*/
static jboolean dryRun = JNI_FALSE; /* initialize VM and exit */
static char *showSettings = NULL; /* print but continue */
static jboolean showResolvedModules = JNI_FALSE;
static jboolean listModules = JNI_FALSE;
static char *describeModule = NULL;
static jboolean validateModules = JNI_FALSE;
static const char *_program_name;
static const char *_launcher_name;
static jboolean _is_java_args = JNI_FALSE;
static jboolean _have_classpath = JNI_FALSE;
static const char *_fVersion;
static jboolean _wc_enabled = JNI_FALSE;
static jboolean dumpSharedSpaces = JNI_FALSE; /* -Xshare:dump */
/*
* Entries for splash screen environment variables.
* putenv is performed in SelectVersion. We need
* them in memory until UnsetEnv, so they are made static
* global instead of auto local.
*/
static char* splash_file_entry = NULL;
static char* splash_jar_entry = NULL;
/*
* List of VM options to be specified when the VM is created.
*/
static JavaVMOption *options;
static int numOptions, maxOptions;
/*
* Prototypes for functions internal to launcher.
*/
static const char* GetFullVersion();
static jboolean IsJavaArgs();
static void SetJavaLauncherProp();
static void SetClassPath(const char *s);
static void SetMainModule(const char *s);
static void SelectVersion(int argc, char **argv, char **main_class);
static jboolean ParseArguments(int *pargc, char ***pargv,
int *pmode, char **pwhat,
int *pret, const char *jrepath);
static jboolean InitializeJVM(JavaVM **pvm, JNIEnv **penv,
InvocationFunctions *ifn);
static jstring NewPlatformString(JNIEnv *env, char *s);
static jclass LoadMainClass(JNIEnv *env, int mode, char *name);
static jclass GetApplicationClass(JNIEnv *env);
static void TranslateApplicationArgs(int jargc, const char **jargv, int *pargc, char ***pargv);
static jboolean AddApplicationOptions(int cpathc, const char **cpathv);
static void SetApplicationClassPath(const char**);
static void PrintJavaVersion(JNIEnv *env);
static void PrintUsage(JNIEnv* env, jboolean doXUsage);
static void ShowSettings(JNIEnv* env, char *optString);
static void ShowResolvedModules(JNIEnv* env);
static void ListModules(JNIEnv* env);
static void DescribeModule(JNIEnv* env, char* optString);
static jboolean ValidateModules(JNIEnv* env);
static void SetPaths(int argc, char **argv);
static void DumpState();
enum OptionKind {
LAUNCHER_OPTION = 0,
LAUNCHER_OPTION_WITH_ARGUMENT,
LAUNCHER_MAIN_OPTION,
VM_LONG_OPTION,
VM_LONG_OPTION_WITH_ARGUMENT,
VM_OPTION
};
static int GetOpt(int *pargc, char ***pargv, char **poption, char **pvalue);
static jboolean IsOptionWithArgument(int argc, char **argv);
/* Maximum supported entries from jvm.cfg. */
#define INIT_MAX_KNOWN_VMS 10
/* Values for vmdesc.flag */
enum vmdesc_flag {
VM_UNKNOWN = -1,
VM_KNOWN,
VM_ALIASED_TO,
VM_WARN,
VM_ERROR,
VM_IF_SERVER_CLASS,
VM_IGNORE
};
struct vmdesc {
char *name;
int flag;
char *alias;
char *server_class;
};
static struct vmdesc *knownVMs = NULL;
static int knownVMsCount = 0;
static int knownVMsLimit = 0;
static void GrowKnownVMs(int minimum);
static int KnownVMIndex(const char* name);
static void FreeKnownVMs();
static jboolean IsWildCardEnabled();
#define SOURCE_LAUNCHER_MAIN_ENTRY "jdk.compiler/com.sun.tools.javac.launcher.SourceLauncher"
/*
* This reports error. VM will not be created and no usage is printed.
*/
#define REPORT_ERROR(AC_ok, AC_failure_message, AC_questionable_arg) \
do { \
if (!AC_ok) { \
JLI_ReportErrorMessage(AC_failure_message, AC_questionable_arg); \
printUsage = JNI_FALSE; \
*pret = 1; \
return JNI_FALSE; \
} \
} while (JNI_FALSE)
#define ARG_CHECK(AC_arg_count, AC_failure_message, AC_questionable_arg) \
do { \
if (AC_arg_count < 1) { \
JLI_ReportErrorMessage(AC_failure_message, AC_questionable_arg); \
printUsage = JNI_TRUE; \
*pret = 1; \
return JNI_TRUE; \
} \
} while (JNI_FALSE)
/*
* Running Java code in primordial thread caused many problems. We will
* create a new thread to invoke JVM. See 6316197 for more information.
*/
static jlong threadStackSize = 0; /* stack size of the new thread */
static jlong maxHeapSize = 0; /* max heap size */
static jlong initialHeapSize = 0; /* initial heap size */
/*
* A minimum initial-thread stack size suitable for most platforms.
* This is the minimum amount of stack needed to load the JVM such
* that it can reject a too small -Xss value. If this is too small
* JVM initialization would cause a StackOverflowError.
*/
#ifndef STACK_SIZE_MINIMUM
#define STACK_SIZE_MINIMUM (64 * KB)
#endif
/*
* Entry point.
*/
JNIEXPORT int JNICALL
JLI_Launch(int argc, char ** argv, /* main argc, argv */
int jargc, const char** jargv, /* java args */
int appclassc, const char** appclassv, /* app classpath */
const char* fullversion, /* full version defined */
const char* dotversion, /* UNUSED dot version defined */
const char* pname, /* program name */
const char* lname, /* launcher name */
jboolean javaargs, /* JAVA_ARGS */
jboolean cpwildcard, /* classpath wildcard*/
jboolean javaw, /* windows-only javaw */
jint ergo /* unused */
)
{
int mode = LM_UNKNOWN;
char *what = NULL;
char *main_class = NULL;
int ret;
InvocationFunctions ifn;
jlong start = 0, end = 0;
char jvmpath[MAXPATHLEN];
char jrepath[MAXPATHLEN];
char jvmcfg[MAXPATHLEN];
_fVersion = fullversion;
_launcher_name = lname;
_program_name = pname;
_is_java_args = javaargs;
_wc_enabled = cpwildcard;
InitLauncher(javaw);
DumpState();
if (JLI_IsTraceLauncher()) {
int i;
printf("Java args:\n");
for (i = 0; i < jargc ; i++) {
printf("jargv[%d] = %s\n", i, jargv[i]);
}
printf("Command line args:\n");
for (i = 0; i < argc ; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
AddOption("-Dsun.java.launcher.diag=true", NULL);
}
/*
* SelectVersion() has several responsibilities:
*
* 1) Disallow specification of another JRE. With 1.9, another
* version of the JRE cannot be invoked.
* 2) Allow for a JRE version to invoke JDK 1.9 or later. Since
* all mJRE directives have been stripped from the request but
* the pre 1.9 JRE [ 1.6 thru 1.8 ], it is as if 1.9+ has been
* invoked from the command line.
*/
SelectVersion(argc, argv, &main_class);
CreateExecutionEnvironment(&argc, &argv,
jrepath, sizeof(jrepath),
jvmpath, sizeof(jvmpath),
jvmcfg, sizeof(jvmcfg));
ifn.CreateJavaVM = 0;
ifn.GetDefaultJavaVMInitArgs = 0;
if (JLI_IsTraceLauncher()) {
start = CurrentTimeMicros();
}
if (!LoadJavaVM(jvmpath, &ifn)) {
return(6);
}
if (JLI_IsTraceLauncher()) {
end = CurrentTimeMicros();
}
JLI_TraceLauncher("%ld micro seconds to LoadJavaVM\n", (long)(end-start));
++argv;
--argc;
if (IsJavaArgs()) {
/* Preprocess wrapper arguments */
TranslateApplicationArgs(jargc, jargv, &argc, &argv);
if (!AddApplicationOptions(appclassc, appclassv)) {
return(1);
}
} else {
/* Set default CLASSPATH */
char* cpath = getenv("CLASSPATH");
if (cpath != NULL) {
SetClassPath(cpath);
}
}
/* Parse command line options; if the return value of
* ParseArguments is false, the program should exit.
*/
if (!ParseArguments(&argc, &argv, &mode, &what, &ret, jrepath)) {
return(ret);
}
/* Override class path if -jar flag was specified */
if (mode == LM_JAR) {
SetClassPath(what); /* Override class path */
}
/* set the -Dsun.java.command pseudo property */
SetJavaCommandLineProp(what, argc, argv);
/* Set the -Dsun.java.launcher pseudo property */
SetJavaLauncherProp();
return JVMInit(&ifn, threadStackSize, argc, argv, mode, what, ret);
}
/*
* Always detach the main thread so that it appears to have ended when
* the application's main method exits. This will invoke the
* uncaught exception handler machinery if main threw an
* exception. An uncaught exception handler cannot change the
* launcher's return code except by calling System.exit.
*
* Wait for all non-daemon threads to end, then destroy the VM.
* This will actually create a trivial new Java waiter thread
* named "DestroyJavaVM", but this will be seen as a different
* thread from the one that executed main, even though they are
* the same C thread. This allows mainThread.join() and
* mainThread.isAlive() to work as expected.
*/
#define LEAVE() \
do { \
if ((*vm)->DetachCurrentThread(vm) != JNI_OK) { \
JLI_ReportErrorMessage(JVM_ERROR2); \
ret = 1; \
} \
if (JNI_TRUE) { \
(*vm)->DestroyJavaVM(vm); \
return ret; \
} \
} while (JNI_FALSE)
#define CHECK_EXCEPTION_NULL_LEAVE(CENL_exception) \
do { \
if ((*env)->ExceptionOccurred(env)) { \
JLI_ReportExceptionDescription(env); \
LEAVE(); \
} \
if ((CENL_exception) == NULL) { \
JLI_ReportErrorMessage(JNI_ERROR); \
LEAVE(); \
} \
} while (JNI_FALSE)
#define CHECK_EXCEPTION_LEAVE(CEL_return_value) \
do { \
if ((*env)->ExceptionOccurred(env)) { \
JLI_ReportExceptionDescription(env); \
ret = (CEL_return_value); \
LEAVE(); \
} \
} while (JNI_FALSE)
#define CHECK_EXCEPTION_FAIL() \
do { \
if ((*env)->ExceptionOccurred(env)) { \
(*env)->ExceptionClear(env); \
return 0; \
} \
} while (JNI_FALSE)
#define CHECK_EXCEPTION_NULL_FAIL(mainObject) \
do { \
if ((*env)->ExceptionOccurred(env)) { \
(*env)->ExceptionClear(env); \
return 0; \
} else if (mainObject == NULL) { \
return 0; \
} \
} while (JNI_FALSE)
/*
* Invoke a static main with arguments. Returns 1 (true) if successful otherwise
* processes the pending exception from GetStaticMethodID and returns 0 (false).
*/
int
invokeStaticMainWithArgs(JNIEnv *env, jclass mainClass, jobjectArray mainArgs) {
jmethodID mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
"([Ljava/lang/String;)V");
CHECK_EXCEPTION_FAIL();
(*env)->CallStaticVoidMethod(env, mainClass, mainID, mainArgs);
return 1;
}
/*
* Invoke an instance main with arguments. Returns 1 (true) if successful otherwise
* processes the pending exception from GetMethodID and returns 0 (false).
*/
int
invokeInstanceMainWithArgs(JNIEnv *env, jclass mainClass, jobjectArray mainArgs) {
jmethodID constructor = (*env)->GetMethodID(env, mainClass, "<init>", "()V");
CHECK_EXCEPTION_FAIL();
jobject mainObject = (*env)->NewObject(env, mainClass, constructor);
CHECK_EXCEPTION_NULL_FAIL(mainObject);
jmethodID mainID = (*env)->GetMethodID(env, mainClass, "main",
"([Ljava/lang/String;)V");
CHECK_EXCEPTION_FAIL();
(*env)->CallVoidMethod(env, mainObject, mainID, mainArgs);
return 1;
}
/*
* Invoke a static main without arguments. Returns 1 (true) if successful otherwise
* processes the pending exception from GetStaticMethodID and returns 0 (false).
*/
int
invokeStaticMainWithoutArgs(JNIEnv *env, jclass mainClass) {
jmethodID mainID = (*env)->GetStaticMethodID(env, mainClass, "main",
"()V");
CHECK_EXCEPTION_FAIL();
(*env)->CallStaticVoidMethod(env, mainClass, mainID);
return 1;
}
/*
* Invoke an instance main without arguments. Returns 1 (true) if successful otherwise
* processes the pending exception from GetMethodID and returns 0 (false).
*/
int
invokeInstanceMainWithoutArgs(JNIEnv *env, jclass mainClass) {
jmethodID constructor = (*env)->GetMethodID(env, mainClass, "<init>", "()V");
CHECK_EXCEPTION_FAIL();
jobject mainObject = (*env)->NewObject(env, mainClass, constructor);
CHECK_EXCEPTION_NULL_FAIL(mainObject);
jmethodID mainID = (*env)->GetMethodID(env, mainClass, "main",
"()V");
CHECK_EXCEPTION_FAIL();
(*env)->CallVoidMethod(env, mainObject, mainID);
return 1;
}
int
JavaMain(void* _args)
{
JavaMainArgs *args = (JavaMainArgs *)_args;
int argc = args->argc;
char **argv = args->argv;
int mode = args->mode;
char *what = args->what;
InvocationFunctions ifn = args->ifn;
JavaVM *vm = 0;
JNIEnv *env = 0;
jclass mainClass = NULL;
jclass appClass = NULL; // actual application class being launched
jobjectArray mainArgs;
int ret = 0;
jlong start = 0, end = 0;
RegisterThread();
/* Initialize the virtual machine */
start = CurrentTimeMicros();
if (!InitializeJVM(&vm, &env, &ifn)) {
JLI_ReportErrorMessage(JVM_ERROR1);
exit(1);
}
if (showSettings != NULL) {
ShowSettings(env, showSettings);
CHECK_EXCEPTION_LEAVE(1);
}
// show resolved modules and continue
if (showResolvedModules) {
ShowResolvedModules(env);
CHECK_EXCEPTION_LEAVE(1);
}
// list observable modules, then exit
if (listModules) {
ListModules(env);
CHECK_EXCEPTION_LEAVE(1);
LEAVE();
}
// describe a module, then exit
if (describeModule != NULL) {
DescribeModule(env, describeModule);
CHECK_EXCEPTION_LEAVE(1);
LEAVE();
}
if (printVersion || showVersion) {
PrintJavaVersion(env);
CHECK_EXCEPTION_LEAVE(0);
if (printVersion) {
LEAVE();
}
}
// modules have been validated at startup so exit
if (validateModules) {
LEAVE();
}
/*
* -Xshare:dump does not have a main class so the VM can safely exit now
*/
if (dumpSharedSpaces) {
CHECK_EXCEPTION_LEAVE(1);
LEAVE();
}
/* If the user specified neither a class name nor a JAR file */
if (printXUsage || printUsage || what == 0 || mode == LM_UNKNOWN) {
PrintUsage(env, printXUsage);
CHECK_EXCEPTION_LEAVE(1);
LEAVE();
}
FreeKnownVMs(); /* after last possible PrintUsage */
if (JLI_IsTraceLauncher()) {
end = CurrentTimeMicros();
JLI_TraceLauncher("%ld micro seconds to InitializeJVM\n", (long)(end-start));
}
/* At this stage, argc/argv have the application's arguments */
if (JLI_IsTraceLauncher()){
int i;
printf("%s is '%s'\n", launchModeNames[mode], what);
printf("App's argc is %d\n", argc);
for (i=0; i < argc; i++) {
printf(" argv[%2d] = '%s'\n", i, argv[i]);
}
}
ret = 1;
/*
* See bugid 5030265. The Main-Class name has already been parsed
* from the manifest, but not parsed properly for UTF-8 support.
* Hence the code here ignores the value previously extracted and
* uses the pre-existing code to reextract the value. This is
* possibly an end of release cycle expedient. However, it has
* also been discovered that passing some character sets through
* the environment has "strange" behavior on some variants of
* Windows. Hence, maybe the manifest parsing code local to the
* launcher should never be enhanced.
*
* Hence, future work should either:
* 1) Correct the local parsing code and verify that the
* Main-Class attribute gets properly passed through
* all environments,
* 2) Remove the vestages of maintaining main_class through
* the environment (and remove these comments).
*
* This method also correctly handles launching existing JavaFX
* applications that may or may not have a Main-Class manifest entry.
*/
mainClass = LoadMainClass(env, mode, what);
CHECK_EXCEPTION_NULL_LEAVE(mainClass);
/*
* In some cases when launching an application that needs a helper, e.g., a
* JavaFX application with no main method, the mainClass will not be the
* applications own main class but rather a helper class. To keep things
* consistent in the UI we need to track and report the application main class.
*/
appClass = GetApplicationClass(env);
CHECK_EXCEPTION_NULL_LEAVE(appClass);
/* Build platform specific argument array */
mainArgs = CreateApplicationArgs(env, argv, argc);
CHECK_EXCEPTION_NULL_LEAVE(mainArgs);
if (dryRun) {
ret = 0;
LEAVE();
}
/*
* PostJVMInit uses the class name as the application name for GUI purposes,
* for example, on OSX this sets the application name in the menu bar for
* both SWT and JavaFX. So we'll pass the actual application class here
* instead of mainClass as that may be a launcher or helper class instead
* of the application class.
*/
PostJVMInit(env, appClass, vm);
CHECK_EXCEPTION_LEAVE(1);
/*
* The main method is invoked here so that extraneous java stacks are not in
* the application stack trace.
*/
if (!invokeStaticMainWithArgs(env, mainClass, mainArgs) &&
!invokeInstanceMainWithArgs(env, mainClass, mainArgs) &&
!invokeStaticMainWithoutArgs(env, mainClass) &&
!invokeInstanceMainWithoutArgs(env, mainClass)) {
ret = 1;
LEAVE();
}
/*
* The launcher's exit code (in the absence of calls to
* System.exit) will be non-zero if main threw an exception.
*/
ret = (*env)->ExceptionOccurred(env) == NULL ? 0 : 1;
LEAVE();
}
/*
* Test if the given name is one of the class path options.
*/
static jboolean
IsClassPathOption(const char* name) {
return JLI_StrCmp(name, "-classpath") == 0 ||
JLI_StrCmp(name, "-cp") == 0 ||
JLI_StrCmp(name, "--class-path") == 0;
}
/*
* Test if the given name is a launcher option taking the main entry point.
*/
static jboolean
IsLauncherMainOption(const char* name) {
return JLI_StrCmp(name, "--module") == 0 ||
JLI_StrCmp(name, "-m") == 0;
}
/*
* Test if the given name is a white-space launcher option.
*/
static jboolean
IsLauncherOption(const char* name) {
return IsClassPathOption(name) ||
IsLauncherMainOption(name) ||
JLI_StrCmp(name, "--describe-module") == 0 ||
JLI_StrCmp(name, "-d") == 0 ||
JLI_StrCmp(name, "--source") == 0;
}
/*
* Test if the given name is a module-system white-space option that
* will be passed to the VM with its corresponding long-form option
* name and "=" delimiter.
*/
static jboolean
IsModuleOption(const char* name) {
return JLI_StrCmp(name, "--module-path") == 0 ||
JLI_StrCmp(name, "-p") == 0 ||
JLI_StrCmp(name, "--upgrade-module-path") == 0 ||
JLI_StrCmp(name, "--add-modules") == 0 ||
JLI_StrCmp(name, "--enable-native-access") == 0 ||
JLI_StrCmp(name, "--limit-modules") == 0 ||
JLI_StrCmp(name, "--add-exports") == 0 ||
JLI_StrCmp(name, "--add-opens") == 0 ||
JLI_StrCmp(name, "--add-reads") == 0 ||
JLI_StrCmp(name, "--patch-module") == 0;
}
static jboolean
IsLongFormModuleOption(const char* name) {
return JLI_StrCCmp(name, "--module-path=") == 0 ||
JLI_StrCCmp(name, "--upgrade-module-path=") == 0 ||
JLI_StrCCmp(name, "--add-modules=") == 0 ||
JLI_StrCCmp(name, "--enable-native-access=") == 0 ||
JLI_StrCCmp(name, "--limit-modules=") == 0 ||
JLI_StrCCmp(name, "--add-exports=") == 0 ||
JLI_StrCCmp(name, "--add-reads=") == 0 ||
JLI_StrCCmp(name, "--patch-module=") == 0;
}
/*
* Test if the given name has a white space option.
*/
jboolean
IsWhiteSpaceOption(const char* name) {
return IsModuleOption(name) ||
IsLauncherOption(name);
}
/*
* Check if it is OK to set the mode.
* If the mode was previously set, and should not be changed,
* a fatal error is reported.
*/
static int
checkMode(int mode, int newMode, const char *arg) {
if (mode == LM_SOURCE) {
JLI_ReportErrorMessage(ARG_ERROR14, arg);
exit(1);
}
return newMode;
}
/*
* Test if an arg identifies a source file.
*/
static jboolean IsSourceFile(const char *arg) {
struct stat st;
return (JLI_HasSuffix(arg, ".java") && stat(arg, &st) == 0);
}
/*
* Checks the command line options to find which JVM type was
* specified. If no command line option was given for the JVM type,
* the default type is used. The environment variable
* JDK_ALTERNATE_VM and the command line option -XXaltjvm= are also
* checked as ways of specifying which JVM type to invoke.
*/
char *
CheckJvmType(int *pargc, char ***argv, jboolean speculative) {
int i, argi;
int argc;
char **newArgv;
int newArgvIdx = 0;
int isVMType;
int jvmidx = -1;
char *jvmtype = getenv("JDK_ALTERNATE_VM");
argc = *pargc;
/* To make things simpler we always copy the argv array */
newArgv = JLI_MemAlloc((argc + 1) * sizeof(char *));
/* The program name is always present */
newArgv[newArgvIdx++] = (*argv)[0];
for (argi = 1; argi < argc; argi++) {
char *arg = (*argv)[argi];
isVMType = 0;
if (IsJavaArgs()) {
if (arg[0] != '-') {
newArgv[newArgvIdx++] = arg;
continue;
}
} else {
if (IsWhiteSpaceOption(arg)) {
newArgv[newArgvIdx++] = arg;
argi++;
if (argi < argc) {
newArgv[newArgvIdx++] = (*argv)[argi];
}
continue;
}
if (arg[0] != '-') break;
}
/* Did the user pass an explicit VM type? */
i = KnownVMIndex(arg);
if (i >= 0) {
jvmtype = knownVMs[jvmidx = i].name + 1; /* skip the - */
isVMType = 1;
*pargc = *pargc - 1;
}
/* Did the user specify an "alternate" VM? */
else if (JLI_StrCCmp(arg, "-XXaltjvm=") == 0 || JLI_StrCCmp(arg, "-J-XXaltjvm=") == 0) {
isVMType = 1;
jvmtype = arg+((arg[1]=='X')? 10 : 12);
jvmidx = -1;
}
if (!isVMType) {
newArgv[newArgvIdx++] = arg;
}
}
/*
* Finish copying the arguments if we aborted the above loop.
* NOTE that if we aborted via "break" then we did NOT copy the
* last argument above, and in addition argi will be less than
* argc.
*/
while (argi < argc) {
newArgv[newArgvIdx++] = (*argv)[argi];
argi++;
}
/* argv is null-terminated */
newArgv[newArgvIdx] = 0;
/* Copy back argv */
*argv = newArgv;
*pargc = newArgvIdx;
/* use the default VM type if not specified (no alias processing) */
if (jvmtype == NULL) {
char* result = knownVMs[0].name+1;
JLI_TraceLauncher("Default VM: %s\n", result);
return result;
}
/* if using an alternate VM, no alias processing */
if (jvmidx < 0)
return jvmtype;
/* Resolve aliases first */
{
int loopCount = 0;
while (knownVMs[jvmidx].flag == VM_ALIASED_TO) {
int nextIdx = KnownVMIndex(knownVMs[jvmidx].alias);
if (loopCount > knownVMsCount) {
if (!speculative) {
JLI_ReportErrorMessage(CFG_ERROR1);
exit(1);
} else {
return "ERROR";
/* break; */
}
}
if (nextIdx < 0) {
if (!speculative) {
JLI_ReportErrorMessage(CFG_ERROR2, knownVMs[jvmidx].alias);
exit(1);
} else {
return "ERROR";
}
}
jvmidx = nextIdx;
jvmtype = knownVMs[jvmidx].name+1;
loopCount++;
}
}
switch (knownVMs[jvmidx].flag) {
case VM_WARN:
if (!speculative) {
JLI_ReportErrorMessage(CFG_WARN1, jvmtype, knownVMs[0].name + 1);
}
/* fall through */
case VM_IGNORE:
jvmtype = knownVMs[jvmidx=0].name + 1;
/* fall through */
case VM_KNOWN:
break;
case VM_ERROR:
if (!speculative) {
JLI_ReportErrorMessage(CFG_ERROR3, jvmtype);
exit(1);
} else {
return "ERROR";
}
}
return jvmtype;
}
/* copied from HotSpot function "atomll()" */
static int
parse_size(const char *s, jlong *result) {
jlong n = 0;
int args_read = sscanf(s, JLONG_FORMAT_SPECIFIER, &n);
if (args_read != 1) {
return 0;
}
while (*s != '\0' && *s >= '0' && *s <= '9') {
s++;
}
// 4705540: illegal if more characters are found after the first non-digit
if (JLI_StrLen(s) > 1) {
return 0;
}
switch (*s) {
case 'T': case 't':
*result = n * GB * KB;
return 1;
case 'G': case 'g':
*result = n * GB;
return 1;
case 'M': case 'm':
*result = n * MB;
return 1;
case 'K': case 'k':
*result = n * KB;
return 1;
case '\0':
*result = n;
return 1;
default:
/* Create JVM with default stack and let VM handle malformed -Xss string*/
return 0;
}
}
/*
* Adds a new VM option with the given name and value.
*/
void
AddOption(char *str, void *info)
{
/*
* Expand options array if needed to accommodate at least one more
* VM option.
*/
if (numOptions >= maxOptions) {
if (options == 0) {
maxOptions = 4;
options = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
} else {
JavaVMOption *tmp;
maxOptions *= 2;
tmp = JLI_MemAlloc(maxOptions * sizeof(JavaVMOption));
memcpy(tmp, options, numOptions * sizeof(JavaVMOption));
JLI_MemFree(options);
options = tmp;
}
}
options[numOptions].optionString = str;
options[numOptions++].extraInfo = info;
/*
* -Xss is used both by the JVM and here to establish the stack size of the thread
* created to launch the JVM. In the latter case we need to ensure we don't go
* below the minimum stack size allowed. If -Xss is zero that tells the JVM to use
* 'default' sizes (either from JVM or system configuration, e.g. 'ulimit -s' on linux),
* and is not itself a small stack size that will be rejected. So we ignore -Xss0 here.
*/
if (JLI_StrCCmp(str, "-Xss") == 0) {
jlong tmp;
if (parse_size(str + 4, &tmp)) {
threadStackSize = tmp;
if (threadStackSize > 0 && threadStackSize < (jlong)STACK_SIZE_MINIMUM) {
threadStackSize = STACK_SIZE_MINIMUM;
}
}
}
if (JLI_StrCCmp(str, "-Xmx") == 0) {
jlong tmp;
if (parse_size(str + 4, &tmp)) {
maxHeapSize = tmp;
}
}
if (JLI_StrCCmp(str, "-Xms") == 0) {
jlong tmp;
if (parse_size(str + 4, &tmp)) {
initialHeapSize = tmp;
}
}
}
static void
SetClassPath(const char *s)
{
char *def;
const char *orig = s;
static const char format[] = "-Djava.class.path=%s";
/*
* usually we should not get a null pointer, but there are cases where
* we might just get one, in which case we simply ignore it, and let the
* caller deal with it
*/
if (s == NULL)
return;
s = JLI_WildcardExpandClasspath(s);
if (sizeof(format) - 2 + JLI_StrLen(s) < JLI_StrLen(s))
// s is became corrupted after expanding wildcards
return;
size_t defSize = sizeof(format)
- 2 /* strlen("%s") */
+ JLI_StrLen(s);
def = JLI_MemAlloc(defSize);
snprintf(def, defSize, format, s);
AddOption(def, NULL);
if (s != orig)
JLI_MemFree((char *) s);