1/*
2 * Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * A minature launcher for use by CustomLauncherTest.sh.  It sets
26 * up the absolute minimal execution environment.
27 */
28#include <stdlib.h>
29#include <strings.h>
30#include <dlfcn.h>
31
32#include "jni.h"
33
34typedef jint (*create_vm_func)(JavaVM **, void**, void*);
35
36void *JNU_FindCreateJavaVM(char *vmlibpath) {
37    void *libVM = dlopen(vmlibpath, RTLD_LAZY);
38    if (libVM == NULL) {
39        return NULL;
40    }
41    return dlsym(libVM, "JNI_CreateJavaVM");
42}
43
44#define CP_PROP  "-Djava.class.path="
45
46int main(int argc, char**argv) {
47     JNIEnv *env;
48     JavaVM *jvm;
49     jint res;
50     jclass cls;
51     jmethodID mid;
52     jstring jstr;
53     jclass stringClass;
54     jobjectArray args;
55     create_vm_func create_vm;
56     JavaVMInitArgs vm_args;
57     char* cp_prop;
58     JavaVMOption options[1];
59
60     if (argc < 4) {
61        fprintf(stderr, "Usage: %s jvm-path classpath class\n", argv[0]);
62        return -1;
63     }
64     cp_prop = (char*)malloc(strlen(CP_PROP)+strlen(argv[2]) +1);
65     sprintf(cp_prop, "%s%s", CP_PROP, argv[2]);
66
67     options[0].optionString = cp_prop;
68     vm_args.version = 0x00010002;
69     vm_args.options = options;
70     vm_args.nOptions = 1;
71     vm_args.ignoreUnrecognized = JNI_TRUE;
72
73     create_vm = (create_vm_func)JNU_FindCreateJavaVM(argv[1]);
74     if (create_vm == NULL) {
75        fprintf(stderr, "can't get address of JNI_CreateJavaVM\n");
76        return -1;
77     }
78
79     res = (*create_vm)(&jvm, (void**)&env, &vm_args);
80     if (res < 0) {
81         fprintf(stderr, "Can't create Java VM\n");
82         return -1;
83     }
84     cls = (*env)->FindClass(env, argv[3]);
85     if (cls == NULL) {
86         goto destroy;
87     }
88
89     mid = (*env)->GetStaticMethodID(env, cls, "main",
90                                     "([Ljava/lang/String;)V");
91     if (mid == NULL) {
92         goto destroy;
93     }
94     jstr = (*env)->NewStringUTF(env, " from C!");
95     if (jstr == NULL) {
96         goto destroy;
97     }
98     stringClass = (*env)->FindClass(env, "java/lang/String");
99     args = (*env)->NewObjectArray(env, 1, stringClass, jstr);
100     if (args == NULL) {
101         goto destroy;
102     }
103     (*env)->CallStaticVoidMethod(env, cls, mid, args);
104
105 destroy:
106     if ((*env)->ExceptionOccurred(env)) {
107         (*env)->ExceptionDescribe(env);
108     }
109     (*jvm)->DestroyJavaVM(jvm);
110
111     return 0;
112 }
113