window JNI_CreateJavaVM启动java程序

https://blog.csdn.net/earbao/article/details/51889605

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2.  
     
  3.  
    #include <stdio.h>
  4.  
    #include "jni.h"
  5.  
    #include <stdlib.h>
  6.  
    #include <windows.h>
  7.  
    #include <tchar.h>
  8.  
     
  9.  
     
  10.  
    //#pragma comment(lib, "jvm.lib")
  11.  
     
  12.  
    #define PATH_SEPARATOR ';' /* define it to be ':' on Solaris */
  13.  
     
  14.  
    #define USER_CLASSPATH "." /* where Prog.class is */
  15.  
     
  16.  
    void testEnv()
  17.  
    {
  18.  
    #ifdef _WIN32
  19.  
     
  20.  
    CHAR dir[ 1024],path[1024];
  21.  
    GetCurrentDirectoryA(MAX_PATH, dir);
  22.  
     
  23.  
    sprintf(path, "set PATH=%s\\jre\\bin\\client;.;", dir);
  24.  
    system(path);
  25.  
    printf("%s\n", path);
  26.  
     
  27.  
     
  28.  
    /**
  29.  
    //set PATH=%PATH%;%currentdir%\jre\bin\client;
  30.  
    TCHAR dir[1024];
  31.  
    GetCurrentDirectoryA(MAX_PATH, dir);
  32.  
     
  33.  
    CHAR p[1024], path[1024];
  34.  
    GetEnvironmentVariableA("PATH", p, 1024);
  35.  
    sprintf("PATH = %s\n", p);
  36.  
     
  37.  
    sprintf(path, "%s;%s\\jre\\bin\\client", p, dir);
  38.  
    system(path);
  39.  
    printf("%s\n", path);
  40.  
     
  41.  
    GetEnvironmentVariableA("PATH", path, 1024);
  42.  
    printf("PATH = %s\n", path);
  43.  
    */
  44.  
     
  45.  
     
  46.  
    #else
  47.  
    char * p;
  48.  
    if ((p = getenv("USER")))
  49.  
    printf("USER = %s\n", p);
  50.  
     
  51.  
    setenv( "USER", "test", 1);
  52.  
    printf("USER = %s\n", getenv("USER"));
  53.  
     
  54.  
    unsetenv( "USER");
  55.  
    printf("USER = %s\n", getenv("USER"));
  56.  
     
  57.  
     
  58.  
     
  59.  
    #endif // __WIN32
  60.  
     
  61.  
     
  62.  
     
  63.  
    }
  64.  
     
  65.  
     
  66.  
    //window JNI_CreateJavaVM启动java程序
  67.  
    int main(int argc, char* argv[])
  68.  
    {
  69.  
    JNIEnv *env;
  70.  
    JavaVM *jvm;
  71.  
    jint res;
  72.  
    jclass cls;
  73.  
    jmethodID mid;
  74.  
    jstring jstr;
  75.  
    jclass stringClass;
  76.  
    jobjectArray args;
  77.  
     
  78.  
    testEnv();
  79.  
    CHAR dir[ 1024], path[1024];
  80.  
    GetCurrentDirectoryA(MAX_PATH, dir);
  81.  
    sprintf(path, "%s\\jre\\bin\\client\\jvm.dll", dir);
  82.  
     
  83.  
    typedef jint (*JNI_CreateJavaVMT)(JavaVM **pvm, void **penv, void *args);
  84.  
    JNI_CreateJavaVMT pJNI_CreateJavaVM = (JNI_CreateJavaVMT)GetProcAddress(LoadLibraryA(path), "JNI_CreateJavaVM");
  85.  
    if (pJNI_CreateJavaVM == NULL)
  86.  
    {
  87.  
    printf("pJNI_CreateJavaVM == NULL\n");
  88.  
    }
  89.  
    else
  90.  
    {
  91.  
    printf("pJNI_CreateJavaVM != NULL\n");
  92.  
     
  93.  
    }
  94.  
     
  95.  
     
  96.  
    #ifdef JNI_VERSION_1_2
  97.  
     
  98.  
    JavaVMInitArgs vm_args;
  99.  
    JavaVMOption options[ 1];
  100.  
    options[ 0].optionString ="-Djava.class.path=.";
  101.  
    vm_args.version = 0x00010002;
  102.  
    vm_args.options = options;
  103.  
    vm_args.nOptions = 1;
  104.  
    vm_args.ignoreUnrecognized = JNI_TRUE;
  105.  
    /* Create the Java VM */
  106.  
    res = pJNI_CreateJavaVM(&jvm, ( void**)&env, &vm_args);
  107.  
    #else
  108.  
    JDK1_1InitArgs vm_args;
  109.  
    char classpath[1024];
  110.  
    vm_args.version = 0x00010001;
  111.  
    JNI_GetDefaultJavaVMInitArgs(&vm_args);
  112.  
    /* Append USER_CLASSPATH to the default system class path */
  113.  
    sprintf(classpath, "%s%c%s",
  114.  
    vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
  115.  
    vm_args.classpath = classpath;
  116.  
    /* Create the Java VM */
  117.  
    res = JNI_CreateJavaVM(&jvm, &env, &vm_args);
  118.  
    #endif /* JNI_VERSION_1_2 */
  119.  
    if (res < 0) {
  120.  
    fprintf(stderr, "Can't create Java VM\n");
  121.  
    exit(1);
  122.  
    }
  123.  
    cls = (*env)->FindClass(env, "Prog");
  124.  
    if (cls == NULL) {
  125.  
    goto destroy;
  126.  
    }
  127.  
    mid = (*env)->GetStaticMethodID(env, cls, "main","([Ljava/lang/String;)V");
  128.  
    if (mid == NULL) {
  129.  
    goto destroy;
  130.  
    }
  131.  
     
  132.  
    jstr = (*env)->NewStringUTF(env, " from C!");
  133.  
    if (jstr == NULL) {
  134.  
    goto destroy;
  135.  
     
  136.  
    }
  137.  
    stringClass = (*env)->FindClass(env, "java/lang/String");
  138.  
    args = (*env)->NewObjectArray(env, 1, stringClass, jstr);
  139.  
    if (args == NULL) {
  140.  
     
  141.  
    goto destroy;
  142.  
     
  143.  
    }
  144.  
    (*env)->CallStaticVoidMethod(env, cls, mid, args);
  145.  
    destroy:
  146.  
     
  147.  
    if ((*env)->ExceptionOccurred(env)) {
  148.  
     
  149.  
    (*env)->ExceptionDescribe(env);
  150.  
     
  151.  
    }
  152.  
     
  153.  
    (*jvm)->DestroyJavaVM(jvm);
  154.  
     
  155.  
    }
  1.  
    import java.io.IOException;
  2.  
     
  3.  
    public class Prog {
  4.  
     
  5.  
    public static void main(String[] args) throws IOException {
  6.  
    //System.out.println(args.length);
  7.  
    if(args.length>0)
  8.  
    {
  9.  
    System.out.println( "Hello World " + args[0]);
  10.  
     
  11.  
    } else{
  12.  
    System.out.println( "Hello World ");
  13.  
     
  14.  
    }
  15.  
    System.in.read();
  16.  
    }
  17.  
     
  18.  
    }

exe通过jni调用Java程序

 

  1.  
    #define _CRT_SECURE_NO_WARNINGS 1
  2.  
     
  3.  
    #include <stdio.h>
  4.  
    #include "jni.h"
  5.  
    #include <stdlib.h>
  6.  
    #include <windows.h>
  7.  
    #include <tchar.h>
  8.  
     
  9.  
    //#pragma comment(lib, "jvm.lib")
  10.  
     
  11.  
    #define PATH_SEPARATOR ';' /* define it to be ':' on Solaris */
  12.  
    #define USER_CLASSPATH "." /* where Prog.class is */
  13.  
     
  14.  
    void testEnv()
  15.  
    {
  16.  
     
  17.  
    CHAR dir[ 1024], path[1024];
  18.  
    GetCurrentDirectoryA(MAX_PATH, dir);
  19.  
    sprintf(path, "set PATH=%s\\jre\\bin\\client;.;", dir);
  20.  
    system(path);
  21.  
    printf("%s\n", path);
  22.  
    sprintf(path, "set PATH=%s\\jre\\bin\\server;.;", dir);
  23.  
    system(path);
  24.  
    printf("%s\n", path);
  25.  
    }
  26.  
     
  27.  
     
  28.  
    //window JNI_CreateJavaVM启动java程序
  29.  
    int main(int argc, char* argv[])
  30.  
    {
  31.  
    JNIEnv *env;
  32.  
    JavaVM *jvm;
  33.  
    jint res;
  34.  
    jclass cls;
  35.  
    jmethodID mid;
  36.  
    jstring jstr;
  37.  
    jclass stringClass;
  38.  
    jobjectArray args;
  39.  
     
  40.  
    //testEnv();
  41.  
    CHAR dir[ 1024], path[1024];
  42.  
    GetCurrentDirectoryA(MAX_PATH, dir);
  43.  
    //注意exe的位数64bit/32bit要和jdk的相匹配
  44.  
    HMODULE handle = NULL;
  45.  
    if (argc > 1)
  46.  
    {
  47.  
    sprintf(path, "%s", argv[1]);
  48.  
    printf("%s\n", path);
  49.  
    handle=LoadLibraryA(path);
  50.  
    }
  51.  
    if (handle == NULL)
  52.  
    {
  53.  
    sprintf(path, "%s\\jre\\bin\\client\\jvm.dll", dir);
  54.  
    printf("%s\n", path);
  55.  
    handle = LoadLibraryA(path);
  56.  
    }
  57.  
    if (handle == NULL)
  58.  
    {
  59.  
    sprintf(path, "%s\\jre\\bin\\server\\jvm.dll", dir);
  60.  
    printf("%s\n", path);
  61.  
    handle = LoadLibraryA(path);
  62.  
    }
  63.  
    if (handle == NULL)
  64.  
    {
  65.  
    handle = LoadLibraryA( "jvm.dll");
  66.  
    }
  67.  
    if (handle == NULL)
  68.  
    {
  69.  
    printf("handle==NULL\n");
  70.  
    return -1;
  71.  
    }
  72.  
     
  73.  
    typedef jint(*JNI_CreateJavaVMT)(JavaVM **pvm, void **penv, void *args);
  74.  
    JNI_CreateJavaVMT pJNI_CreateJavaVM = (JNI_CreateJavaVMT)GetProcAddress(handle, "JNI_CreateJavaVM");
  75.  
    if (pJNI_CreateJavaVM == NULL)
  76.  
    {
  77.  
    printf("pJNI_CreateJavaVM == NULL\n");
  78.  
    }
  79.  
    else
  80.  
    {
  81.  
    printf("pJNI_CreateJavaVM != NULL\n");
  82.  
     
  83.  
    }
  84.  
    //初始化jvm参数 http://blog.csdn.net/louka/article/details/7101562
  85.  
    JavaVMInitArgs vm_args;
  86.  
    JavaVMOption options[ 2];
  87.  
    //搜索的类路径。把java类和jar包放在当前目录下
  88.  
    #if _WIN32
  89.  
    options[ 0].optionString = "-Djava.class.path=.;test.jar"; //这里指定了要使用的第三方Jar包
  90.  
    #else
  91.  
    options[ 0].optionString = "-Djava.class.path=.:test.jar"; //这里指定了要使用的第三方Jar包
  92.  
    #endif
  93.  
     
  94.  
    options[ 1].optionString = "-verbose:NONE"; //用于跟踪运行时的信息
  95.  
     
  96.  
    vm_args.version = 0x00010002;
  97.  
    vm_args.options = options;
  98.  
    vm_args.nOptions = 1;
  99.  
    vm_args.ignoreUnrecognized = JNI_TRUE;
  100.  
    /* Create the Java VM */
  101.  
    res = pJNI_CreateJavaVM(&jvm, ( void**)&env, &vm_args);
  102.  
     
  103.  
    if (res < 0) {
  104.  
    fprintf(stderr, "Can't create Java VM\n");
  105.  
    exit(1);
  106.  
    }
  107.  
    cls = (*env)->FindClass(env, "Prog");
  108.  
    if (cls == NULL) {
  109.  
    goto destroy;
  110.  
    }
  111.  
    mid = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String;)V");
  112.  
    if (mid == NULL) {
  113.  
    goto destroy;
  114.  
    }
  115.  
     
  116.  
    jstr = (*env)->NewStringUTF(env, " from C!");
  117.  
    if (jstr == NULL) {
  118.  
    goto destroy;
  119.  
     
  120.  
    }
  121.  
    stringClass = (*env)->FindClass(env, "java/lang/String");
  122.  
    args = (*env)->NewObjectArray(env, 1, stringClass, jstr);
  123.  
    if (args == NULL) {
  124.  
    goto destroy;
  125.  
    }
  126.  
    (*env)->CallStaticVoidMethod(env, cls, mid, args);
  127.  
    (*env)->DeleteLocalRef(env, cls);
  128.  
    (*env)->DeleteLocalRef(env, jstr);
  129.  
    (*env)->DeleteLocalRef(env, stringClass);
  130.  
    (*env)->DeleteLocalRef(env, args);
  131.  
     
  132.  
    destroy:
  133.  
    if ((*env)->ExceptionOccurred(env)) {
  134.  
    (*env)->ExceptionDescribe(env);
  135.  
    }
  136.  
    (*jvm)->DestroyJavaVM(jvm);
  137.  
    FreeLibrary(handle);
  138.  
    }
  139.  
     


源码下载(右键另存为zip):

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值