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