Classroom teacher said, native interface can achieve the mixed language, to make up for the deficiencies of the Java language, hear here I am overjoyed, I thought I can use my C + +, haha, happy, joy, I returned to the dormitory at night to try this native interface function.
First find the following code from the Internet:
1. Java is compiled first as a. class file:
public class HelloWorld {public native void Displayhelloworld (); static {system.loadlibrary (' HelloWorld ');} public stat IC void Main (string[] args) {new HelloWorld (). Displayhelloworld ();}}
2. Suppose the Helloworld.class directory is F:/javacode/native/bin/helloworld.class
Then enter Javah-classpath-jni F:/javacode/native/bin/helloworld in the root directory via cmd
Can output HelloWorld.h header file, the following to use, Haha, good custody;
3. Here are some HelloWorld.cpp files, I like to use the VC6.0 here, the establishment of the dynamic link library file, not the console oh, careful,
Content is:
#include <stdio.h>
#include <jni.h>
#include "HelloWorld.h"
Jniexport void Jnicall
Java_helloworld_displayhelloworld (jnienv *env, Jobject obj)
{
printf ("Hello world!/n");
Return
}
The jni.h here is when the JDK is installed in its root directory, assuming that you are the default installation under the C:/Program Files/java/jdk1.6.0_10/include folder, through the
The Directories tab in Tools->options adds the directory where the JNI resides, as shown in the screenshot below.
4. After compiling, execute can get our dream of HelloWorld.dll file, hey, hehe ready to work here even finished, right, but also to the HelloWorld.dll file
Copy to the same folder as the HelloWorld.dll file, and then enter the Java HelloWorld into its folder by entering the command prompt, cmd
Haha, see. Hello world! The first Java and C mixed program in your life was born, happy, haha, enjoy the charm of language.