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):

window JNI_CreateJavaVM启动java程序的更多相关文章

  1. Android For JNI(一)——JNI的概念以及C语言开发工具dev-c++,编写你的第一个C语言程序,使用C启动JAVA程序

    Android For JNI(一)--JNI的概念以及C语言开发工具dev-c++,编写你的第一个C语言程序 当你的Android之旅一步步的深入的时候,你其实会发现,很多东西都必须去和framew ...

  2. Linux上设置开机启动Java程序

    在Linux上设置开机启动Java程序,例如:test.jar 在Linux上启动Java程序的命令: nohup java -jar test.jar >/dev/>& & ...

  3. eclipse通过maven建立java se工程配置log4j,打包成zip,将jar包和配置文件分开,并以bat和sh文件启动java程序

    一.新建maven的java工程 1.eclipse里file-new-other,选择maven Project 2.选中 Use default Workspace location,然后 nex ...

  4. 利用脚本启动java程序

    今天在工作中,需要写一个shell脚本,启动一个socket程序,从而模拟短信网关.查了一些资料,终于搞定了,现在记录一下,方便大家查阅. 为了说明使用方法,我们就用最简单的程序来实现,比如我们要运行 ...

  5. ActiveMQ(下载,启动,java程序中 如何操作)

    为了快速上手ActiveMQ 找个一个windows版本的mq来实现它的功能 1.http://activemq.apache.org/activemq-5158-release.html 下载 2. ...

  6. shell脚本启动java程序

    #!/bin/bash ### 切换到工作目录 bin=$(cd `dirname ${0}`;pwd) cd ${bin} echo "bin [${bin}] .." ### ...

  7. java_linux_shell_定时kill 启动java程序

    #!/bin/bash #while truedo Process_ID=`ps -ef |grep 'LoginSinaWeiboCookie.jar' |grep -v grep |awk '{p ...

  8. Shell 启动java程序

    #!/bin/sh SHELL_PATH=$(cd ")";pwd) echo $SHELL_PATH cd "$SHELL_PATH" CLASSPATH=. ...

  9. Window 无法启动此程序,因为计算机中丢失api-ms-win-crt-runtime-l1-1-0.dll。尝试重新安装该程序以解决此问题。

    现象: 解决办法: 方法一:缺什么补什么 http://www.greenxf.com/soft/125654.html 把api-ms-win-crt-runtime-l1-1-0.dll下载到电脑 ...

随机推荐

  1. golang 中处理大规模tcp socket网络连接的方法,相当于c语言的 poll 或 epoll

    https://groups.google.com/forum/#!topic/golang-nuts/I7a_3B8_9Gw https://groups.google.com/forum/#!ms ...

  2. 万恶之源 - Python 自定义模块

    自定义模块 我们今天来学习一下自定义模块(也就是私人订制),我们要自定义模块,首先就要知道什么是模块啊 一个函数封装一个功能,比如现在有一个软件,不可能将所有程序都写入一个文件,所以咱们应该分文件,组 ...

  3. Linux命令:xargs命令详解,xargs与管道的区别

    阅读目录 为什么要用xargs,问题的来源 xargs是什么,与管道有什么不同 xargs的一些有用的选项 回到顶部 为什么要用xargs,问题的来源 在工作中经常会接触到xargs命令,特别是在别人 ...

  4. maven配置本地仓库通用

    只要在settings.xml文件中指定仓库就可以了,然后复制仓库到任何地方都可以使用,eclipse中指定一个settings.xml就可以了 仓库的位置是.locks所在目录

  5. TL-WAR1200L V1.0升级软件20170609

         TL-WAR1200L_V1.0升级软件20170609.part1.rar  TL-WAR1200L_V1.0升级软件20170609.part2.rar TP-LINK WVR& ...

  6. jenkins 添加 证书凭证Credentials

    jenkins 添加 证书凭证Credentials 大家都知道jenkins在拉取git项目代码的时候,如果没有配置 “证书凭证Credentials” 或者配置的不对, 就会出现红色报错,最终导致 ...

  7. c++基础:之封装

         原创: 零灵柒 C/C++的编程教室 2月4日 什么是类 C++是什么?C++设计之初就是class with c,所以简单点说,C++就是带类的C,那么什么是类? 类,简单点说就是类型,在 ...

  8. CSU 1838 Water Pump(单调栈)

    Water Pump [题目链接]Water Pump [题目类型]单调栈 &题解: 这题可以枚举缺口,共n-1个,之后把前缀面积和后缀面积用O(n)打一下表,最后总面积减去前缀的i个和后缀的 ...

  9. java.lang.NoClassDefFoundError: org/hibernate/QueryTimeoutException

    在做ssh整合的时候报错:java.lang.NoClassDefFoundError: org/hibernate/QueryTimeoutException org.springframework ...

  10. EL的隐含对象(一)【页面上下文对象】

    页面上下文对象为pageContext,用于访问JSP内置对象(例如:request.response.out.session.exception.page等)和ServletContext.在获取到 ...