Path: blob/master/test/jdk/sun/management/jmxremote/bootstrap/exelauncher.c
41153 views
/*1* Copyright (c) 2006, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* A miniature launcher for use by CustomLauncherTest.java test. It sets25* up the absolute minimal execution environment.26*/27#include <stdlib.h>28#include <string.h>29#include <dlfcn.h>3031#include "jni.h"3233typedef jint (*create_vm_func)(JavaVM **, void**, void*);3435void *JNU_FindCreateJavaVM(char *vmlibpath) {36void *libVM = dlopen(vmlibpath, RTLD_LAZY);37if (libVM == NULL) {38return NULL;39}40return dlsym(libVM, "JNI_CreateJavaVM");41}4243#define CP_PROP "-Djava.class.path="4445int main(int argc, char**argv) {46JNIEnv *env;47JavaVM *jvm;48jint res;49jclass cls;50jmethodID mid;51jstring jstr;52jclass stringClass;53jobjectArray args;54create_vm_func create_vm;55JavaVMInitArgs vm_args;56char* cp_prop;57JavaVMOption options[1];5859if (argc < 4) {60fprintf(stderr, "Usage: %s jvm-path classpath class\n", argv[0]);61return -1;62}63cp_prop = (char*)malloc(strlen(CP_PROP)+strlen(argv[2]) +1);64sprintf(cp_prop, "%s%s", CP_PROP, argv[2]);6566options[0].optionString = cp_prop;67vm_args.version = 0x00010002;68vm_args.options = options;69vm_args.nOptions = 1;70vm_args.ignoreUnrecognized = JNI_TRUE;7172create_vm = (create_vm_func)JNU_FindCreateJavaVM(argv[1]);73if (create_vm == NULL) {74fprintf(stderr, "can't get address of JNI_CreateJavaVM\n");75return -1;76}7778res = (*create_vm)(&jvm, (void**)&env, &vm_args);79if (res < 0) {80fprintf(stderr, "Can't create Java VM\n");81return -1;82}83cls = (*env)->FindClass(env, argv[3]);84if (cls == NULL) {85goto destroy;86}8788mid = (*env)->GetStaticMethodID(env, cls, "main",89"([Ljava/lang/String;)V");90if (mid == NULL) {91goto destroy;92}93jstr = (*env)->NewStringUTF(env, " from C!");94if (jstr == NULL) {95goto destroy;96}97stringClass = (*env)->FindClass(env, "java/lang/String");98args = (*env)->NewObjectArray(env, 1, stringClass, jstr);99if (args == NULL) {100goto destroy;101}102(*env)->CallStaticVoidMethod(env, cls, mid, args);103104destroy:105if ((*env)->ExceptionOccurred(env)) {106(*env)->ExceptionDescribe(env);107}108(*jvm)->DestroyJavaVM(jvm);109110return 0;111}112113114