Well sometimes it is necessary to create functions that are dependent System and therefore can not be implemented in Java, that's what JNI (Java Native Interface), used to call native functions of the system and can be in a DLL (in Windows). Well now we will see how to create a native function that is being called in Java. The first thing we do is create a class, and has defined a native method, in turn, charge a class library that is called "Project1" (that's how I got my C compiler hahaha) then the main , instantiate the object and call the function:
public class HelloWorld {public
native void sayHello ();
{static
System.loadLibrary ("Project1");
} public static void main (String [] args) {
new HelloWorld (). SayHello ();
}}
The example is very simple, we only have one class with a native method, a static block to load a library named Project1 and a main, which creates the instance and sends método.Y call now just compile the class: javac HelloWorld.java
Now we have to do is create a C header, which contains the definition of the role we have to implement, Java has a tool for this and then go to the command line again and type:
javah HelloWorld
Go to the folder where there was our class and we can see that there is a file called HelloWorld.h
/ * DO NOT EDIT THIS FILE - it is machine generated * / # include
/ * Header for class HelloWorld * / # ifndef _Included_HelloWorld
# define _Included_HelloWorld
# ifdef __cplusplus extern "C" {# endif
/ *
* Class: HelloWorld
* Method: sayHello
* Signature: () V
* / JNIEXPORT void JNICALL Java_HelloWorld_sayHello
(JNIEnv *, jobject);
# ifdef __cplusplus}
# endif # endif
This Header contains the definition of the function to be called the library, now we just have to create an implementation that will look more or less like this:
# include # include "HelloWorld.h"
# include
JNIEXPORT void JNICALL
Java_HelloWorld_sayHello (JNIEnv * env, jobject obj)
{printf ("Hello world Im a C Function \\ n");
return;}
And we have to compile it in a DLL, which is the part a bit tedious but if you've ever used a compiler for C or C + + has no big problem, you can use Borland Blodshed Dev C + + or Visual C + +. I compile with Blodshed Dev C + + and here are the instructions:
1 .- Once you have the file HelloWorld.h opens the Bloodshed Dev C + + and create a new project in the same folder where the file, the project type specified as DLL. (Here's because my dll is called Project1 haha)
2 .- Go to Tools-> Compiler Options, when you open the dialog box, go to the Directories tab and then to C + + Includes. (Or C includes the type of project you've created) Ahi click on the button that is like a doily, and go to where your Java is installed and add the following folders $ JAVA_HOME \\ include and $ JAVA_HOME \\ include \\ win32 , where $ JAVA_HOME = directory where you installed Java in my case: C: \\ Program Files \\ Java \\ jdk1.6.0_11, then my routes are as follows:
C: \\ Program Files \\ Java \\ jdk1.6.0_11 \\ include
C: \\ Program Files \\ Java \\ jdk1.6.0_11 \\ include \\ win32
3 .-. cpp file that automatically creates the IDE copy the following code:
# include # include "HelloWorld.h"
# include
JNIEXPORT void JNICALL
Java_HelloWorld_sayHello (JNIEnv * env, jobject obj)
{
printf ("Hello world Im a C + + \\ n");
return;
}
and go to Execute-> Compile , and your program should compile and generate a file named "Project1.dll" (or whatever you've placed the project.
Then just go to command line again and run: java
HelloWorld
And the result must be: Hello world
Im a C Function
And so you call a native function: D Congratulations!
I hope you have served
Greetings!
0 comments:
Post a Comment