With MSVC6, create a new Win32 DLL project (simple) and call it javahowto. In the same directory create a java source called JavaHowTo.java class JavaHowTo { public native String sayHello(); static { System.loadLibrary("javahowto"); } }
Compile the Java program and use javah utility to generate the JavaHowTo.h header file. javah -jni JavaHowTo In MSVC6, add the JavaHowTo.h in your project header files In the Tools - Options menu, set the include directories to include the Java JNI headers files. They are located in [jdk dir]\include and [jdk dir]\include\win32 directories In the javahowto.cpp source, add #include "JavaHowTo.h" JNIEXPORT jstring JNICALL Java_JavaHowTo_sayHello (JNIEnv *env, jobject obj) { return env->NewStringUTF("Hello world"); }
Select the Release configuration and build the project. Copy the javahowto.dll in the same directory as the java program. Create this new java program public class JNIJavaHowTo { public static void main(String[] args) { JavaHowTo jht = new JavaHowTo(); System.out.println(jht.sayHello()); } }
In the same directory create a java source called JavaHowTo.java class JavaHowTo {
public native String sayHello();
static {
System.loadLibrary("javahowto");
}
}
Compile the Java program and use javah utility to generate the JavaHowTo.h header file. javah -jni JavaHowTo
In MSVC6, add the JavaHowTo.h in your project header files
In the Tools - Options menu, set the include directories to include the Java JNI headers files. They are located in [jdk dir]\include and [jdk dir]\include\win32 directories
In the javahowto.cpp source, add #include "JavaHowTo.h" JNIEXPORT jstring JNICALL Java_JavaHowTo_sayHello
(JNIEnv *env, jobject obj) {
return env->NewStringUTF("Hello world");
}
Select the Release configuration and build the project.
Copy the javahowto.dll in the same directory as the java program.
Create this new java program public class JNIJavaHowTo {
public static void main(String[] args) {
JavaHowTo jht = new JavaHowTo();
System.out.println(jht.sayHello());
}
}
Compile and execute.
http://java.sun.com/j2se/1.3/docs/guide/jni/index.html
对不起,我们不使用微软的windows
to all
JNI在一般书中只介绍C调用Java类,但对Java类调用C不介绍(至少我看的几本书没介绍),如果大虾知道如何做,请详细说明.
{
public native boolean install_hook();
public native boolean uninstall_hook();
public native String get_hook_result();
String rs="";
protected Thread thread;
static
{
System.loadLibrary("jhook");
} public javahook()
{
} public void run()
{
install_hook(); //install the hook
while(true)
{
rs=get_hook_result();//这个就是调用c中的方法(装在dll中)
System.out.println("....."+rs+".....");
if(rs=="exit") break; try {
// if(rs=="") Thread.sleep(100);
Thread.sleep(50);
} catch (Exception e) {}
}
uninstall_hook(); //uninstall the hook
}
public String get_result()
{
return rs;
}
}看sun的文档说明!!
这个方法我听说过,但没有试验,谢谢!