When generated under Eclipse, it is important to note that, since the class is usually added in the package, rather than being added separately as a separate project, it is necessary to go into the folder where the packages are located, javac the package name , and compile it in the same way. The Javah command is the same.
First create two Java files:
Getpara.java:
1 Package Com.niiam; 2 3 Public class Getpara {4 Public native Double getparameter (); 5 }
This establishes a class in which a locally implemented method, GetParameter (), is defined in the class, which does not pass in parameters, outgoing a fixed double type data, and is later implemented by the C + + language
Set up a Test.java to test:
1 PackageCom.niiam;2 3 Public classTest {4 Public Static voidMain (string[] args) {5System.loadlibrary ("Getpara");6Getpara para=NewGetpara ();7System.out.println ("The height is:" +Para.getparameter ());8 }9}
Attention!!!
Here is the Getpara.dll link library that we are going to generate in the main method.
The statement can also be placed inside the Getpara class and decorated with static{}, so that each time the class is instantiated, it is equivalent to implementing the introduction of library operations.
In conjunction with the Springboot project to use the second way, remember!
Enter the folder where the Com.niiam package is located
Javac Com.niiam.GetPara.java
Javac Com.niiam.Test.java to compile
Javah Com.niiam.GetPara.java generating. h files Com_niiam_getpara.h
Note that the. h file here is a function declaration that contains the name of the package, within it:
Jniexport jdouble Jnicall Java_com_niiam_getpara_getparameter
(JNIENV *, jobject);
Also has the package name exists, differs from the manual establishment project situation, the generated DLL file is also different, cannot equal
Setting up the console project in VS, output set to DLL
Introduce a header file that introduces the directory where the jni.h jni_md.h is in the included directory
Create a Getpara.cpp file
1 " com_niiam_getpara.h " 2 3 4 5 jniexport jdouble jnicall java_com_niiam_getpara_getparameter 6 (jnienv *, Jobject) 7{ 8 double104.06 ; 9 return A; Ten }
Compiling build DLL files
Note that because Java platform is a x64 platform, the DLL files generated here must also be x64
Where to put the generated DLL files, here are a few options:
1, put in the disk development directory, the directory is added to the environment variable path, so that the runtime can be introduced. This approach is also appropriate for running in Eclipse and running Java Com.niiam.GetPara in the console
2, put in the project directory, and add the project directory into the Java build path,
Or is:
Tested, put them in these two places.
This will directly work directly in Eclipse.
A simple JNI program implemented under Eclipse returns a double type