window JNI_CreateJavaVM启动java程序

时间:2024-04-08 22:33:44

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

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

exe通过jni调用Java程序

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

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

window JNI_CreateJavaVM启动java程序