https://blog.csdn.net/earbao/article/details/51889605
-
-
-
-
-
-
-
-
-
-
//#pragma comment(lib, "jvm.lib")
-
-
-
-
-
-
void testEnv()
-
{
-
-
-
CHAR dir[1024],path[1024];
-
GetCurrentDirectoryA(MAX_PATH, dir);
-
-
sprintf(path, "set PATH=%s\jre\bin\client;.;", dir);
-
system(path);
-
printf("%s ", path);
-
-
-
/**
-
//set PATH=%PATH%;%currentdir%jreinclient;
-
TCHAR dir[1024];
-
GetCurrentDirectoryA(MAX_PATH, dir);
-
-
CHAR p[1024], path[1024];
-
GetEnvironmentVariableA("PATH", p, 1024);
-
sprintf("PATH = %s ", p);
-
-
sprintf(path, "%s;%s\jre\bin\client", p, dir);
-
system(path);
-
printf("%s ", path);
-
-
GetEnvironmentVariableA("PATH", path, 1024);
-
printf("PATH = %s ", path);
-
*/
-
-
-
-
char * p;
-
if ((p = getenv("USER")))
-
printf("USER = %s ", p);
-
-
setenv("USER", "test", 1);
-
printf("USER = %s ", getenv("USER"));
-
-
unsetenv("USER");
-
printf("USER = %s ", getenv("USER"));
-
-
-
-
-
-
-
-
}
-
-
-
//window JNI_CreateJavaVM启动java程序
-
int main(int argc, char* argv[])
-
{
-
JNIEnv *env;
-
JavaVM *jvm;
-
jint res;
-
jclass cls;
-
jmethodID mid;
-
jstring jstr;
-
jclass stringClass;
-
jobjectArray args;
-
-
testEnv();
-
CHAR dir[1024], path[1024];
-
GetCurrentDirectoryA(MAX_PATH, dir);
-
sprintf(path, "%s\jre\bin\client\jvm.dll", dir);
-
-
typedef jint (*JNI_CreateJavaVMT)(JavaVM **pvm, void **penv, void *args);
-
JNI_CreateJavaVMT pJNI_CreateJavaVM = (JNI_CreateJavaVMT)GetProcAddress(LoadLibraryA(path), "JNI_CreateJavaVM");
-
if (pJNI_CreateJavaVM == NULL)
-
{
-
printf("pJNI_CreateJavaVM == NULL ");
-
}
-
else
-
{
-
printf("pJNI_CreateJavaVM != NULL ");
-
-
}
-
-
-
-
-
JavaVMInitArgs vm_args;
-
JavaVMOption options[1];
-
options[0].optionString ="-Djava.class.path=.";
-
vm_args.version = 0x00010002;
-
vm_args.options = options;
-
vm_args.nOptions = 1;
-
vm_args.ignoreUnrecognized = JNI_TRUE;
-
/* Create the Java VM */
-
res = pJNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
-
-
JDK1_1InitArgs vm_args;
-
char classpath[1024];
-
vm_args.version = 0x00010001;
-
JNI_GetDefaultJavaVMInitArgs(&vm_args);
-
/* Append USER_CLASSPATH to the default system class path */
-
sprintf(classpath, "%s%c%s",
-
vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
-
vm_args.classpath = classpath;
-
/* Create the Java VM */
-
res = JNI_CreateJavaVM(&jvm, &env, &vm_args);
-
-
if (res < 0) {
-
fprintf(stderr, "Can't create Java VM ");
-
exit(1);
-
}
-
cls = (*env)->FindClass(env, "Prog");
-
if (cls == NULL) {
-
goto destroy;
-
}
-
mid = (*env)->GetStaticMethodID(env, cls, "main","([Ljava/lang/String;)V");
-
if (mid == NULL) {
-
goto destroy;
-
}
-
-
jstr = (*env)->NewStringUTF(env, " from C!");
-
if (jstr == NULL) {
-
goto destroy;
-
-
}
-
stringClass = (*env)->FindClass(env, "java/lang/String");
-
args = (*env)->NewObjectArray(env, 1, stringClass, jstr);
-
if (args == NULL) {
-
-
goto destroy;
-
-
}
-
(*env)->CallStaticVoidMethod(env, cls, mid, args);
-
destroy:
-
-
if ((*env)->ExceptionOccurred(env)) {
-
-
(*env)->ExceptionDescribe(env);
-
-
}
-
-
(*jvm)->DestroyJavaVM(jvm);
-
-
}
-
import java.io.IOException;
-
-
public class Prog {
-
-
public static void main(String[] args) throws IOException {
-
//System.out.println(args.length);
-
if(args.length>0)
-
{
-
System.out.println("Hello World " + args[0]);
-
-
}else{
-
System.out.println("Hello World ");
-
-
}
-
System.in.read();
-
}
-
-
}
exe通过jni调用Java程序
-
-
-
-
-
-
-
-
-
//#pragma comment(lib, "jvm.lib")
-
-
-
-
-
void testEnv()
-
{
-
-
CHAR dir[1024], path[1024];
-
GetCurrentDirectoryA(MAX_PATH, dir);
-
sprintf(path, "set PATH=%s\jre\bin\client;.;", dir);
-
system(path);
-
printf("%s ", path);
-
sprintf(path, "set PATH=%s\jre\bin\server;.;", dir);
-
system(path);
-
printf("%s ", path);
-
}
-
-
-
//window JNI_CreateJavaVM启动java程序
-
int main(int argc, char* argv[])
-
{
-
JNIEnv *env;
-
JavaVM *jvm;
-
jint res;
-
jclass cls;
-
jmethodID mid;
-
jstring jstr;
-
jclass stringClass;
-
jobjectArray args;
-
-
//testEnv();
-
CHAR dir[1024], path[1024];
-
GetCurrentDirectoryA(MAX_PATH, dir);
-
//注意exe的位数64bit/32bit要和jdk的相匹配
-
HMODULE handle = NULL;
-
if (argc > 1)
-
{
-
sprintf(path, "%s", argv[1]);
-
printf("%s ", path);
-
handle=LoadLibraryA(path);
-
}
-
if (handle == NULL)
-
{
-
sprintf(path, "%s\jre\bin\client\jvm.dll", dir);
-
printf("%s ", path);
-
handle = LoadLibraryA(path);
-
}
-
if (handle == NULL)
-
{
-
sprintf(path, "%s\jre\bin\server\jvm.dll", dir);
-
printf("%s ", path);
-
handle = LoadLibraryA(path);
-
}
-
if (handle == NULL)
-
{
-
handle = LoadLibraryA("jvm.dll");
-
}
-
if (handle == NULL)
-
{
-
printf("handle==NULL ");
-
return -1;
-
}
-
-
typedef jint(*JNI_CreateJavaVMT)(JavaVM **pvm, void **penv, void *args);
-
JNI_CreateJavaVMT pJNI_CreateJavaVM = (JNI_CreateJavaVMT)GetProcAddress(handle, "JNI_CreateJavaVM");
-
if (pJNI_CreateJavaVM == NULL)
-
{
-
printf("pJNI_CreateJavaVM == NULL ");
-
}
-
else
-
{
-
printf("pJNI_CreateJavaVM != NULL ");
-
-
}
-
//初始化jvm参数 http://blog.csdn.net/louka/article/details/7101562
-
JavaVMInitArgs vm_args;
-
JavaVMOption options[2];
-
//搜索的类路径。把java类和jar包放在当前目录下
-
-
options[0].optionString = "-Djava.class.path=.;test.jar"; //这里指定了要使用的第三方Jar包
-
-
options[0].optionString = "-Djava.class.path=.:test.jar"; //这里指定了要使用的第三方Jar包
-
-
-
options[1].optionString = "-verbose:NONE"; //用于跟踪运行时的信息
-
-
vm_args.version = 0x00010002;
-
vm_args.options = options;
-
vm_args.nOptions = 1;
-
vm_args.ignoreUnrecognized = JNI_TRUE;
-
/* Create the Java VM */
-
res = pJNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
-
-
if (res < 0) {
-
fprintf(stderr, "Can't create Java VM ");
-
exit(1);
-
}
-
cls = (*env)->FindClass(env, "Prog");
-
if (cls == NULL) {
-
goto destroy;
-
}
-
mid = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String;)V");
-
if (mid == NULL) {
-
goto destroy;
-
}
-
-
jstr = (*env)->NewStringUTF(env, " from C!");
-
if (jstr == NULL) {
-
goto destroy;
-
-
}
-
stringClass = (*env)->FindClass(env, "java/lang/String");
-
args = (*env)->NewObjectArray(env, 1, stringClass, jstr);
-
if (args == NULL) {
-
goto destroy;
-
}
-
(*env)->CallStaticVoidMethod(env, cls, mid, args);
-
(*env)->DeleteLocalRef(env, cls);
-
(*env)->DeleteLocalRef(env, jstr);
-
(*env)->DeleteLocalRef(env, stringClass);
-
(*env)->DeleteLocalRef(env, args);
-
-
destroy:
-
if ((*env)->ExceptionOccurred(env)) {
-
(*env)->ExceptionDescribe(env);
-
}
-
(*jvm)->DestroyJavaVM(jvm);
-
FreeLibrary(handle);
-
}
-
源码下载(右键另存为zip):