...;C:\Program Files (x86)\Java\jre7\bin;C:\Program Files (x86)\Java\jre7\bin\client;
package com.uuorange.example;
import javax.swing.JOptionPane;
public class MainScene
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null, "MainScene.main() was called!", "Tip", JOptionPane.INFORMATION_MESSAGE);
}
public static void fun()
{
JOptionPane.showMessageDialog(null, "MainScene.fun() was called!", "Tip", JOptionPane.INFORMATION_MESSAGE);
}
}
// FunCall_C2Java_Caller.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "jni.h"
int _tmain(int argc, _TCHAR* argv[])
{
JavaVM *jvm; /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */
JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
JavaVMOption* options = new JavaVMOption[3];
options[0].optionString = "-Djava.compiler=NONE";
options[1].optionString = "-Djava.class.path=e:/FunCall_C2Java_Callee.jar";
options[2].optionString = "-verbose:jni";
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 3;
vm_args.options = options;
vm_args.ignoreUnrecognized = false;
/* load and initialize a Java VM, return a JNI interface
* pointer in env */
JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
delete options;
/* invoke the Main.test method using the JNI */
jclass cls = env->FindClass("com/uuorange/example/MainScene");
jmethodID mid = env->GetStaticMethodID(cls, "fun", "()V");
env->CallStaticVoidMethod(cls, mid, 100);
/* We are done. */
jvm->DestroyJavaVM();
return 0;
}
执行生成的exe文件,弹出提示框“MainScene.fun() was called!”说明调用成功
http://stackoverflow.com/questions/819536/how-to-call-java-function-from-chttp://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/invocation.html
代码下载:http://download.csdn.net/detail/bsxylj/8140897