Download presentation
Presentation is loading. Please wait.
Published byRachel Craig Modified over 9 years ago
1
By Noorez Kassam Welcome to JNI
2
Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather not rewrite it in Java. Instead, you would like to use the existing code base. 2. You need to access system devices or perform some platform- specific task that is beyond the capability of Java. Many Java library routines eventually invoke private methods for just this purpose. For instance, the I/O, threading, and networking packages all contain private native methods. 3. You think that Java might be too slow for your application and that performance can be enhanced by implementing time-critical code in C++.
3
Differences between Java and C++ In Java everything must belong to class, but in c++, this is not always the case. A character in java is the Unicode 2-byte char, but in c++ a char is the ASCII 1-byte char. In Java, all primitives are stack allocated and are passed by value, objects are dynamically allocated by using new and are always passed by reference. In C++ everything is stack allocated and passed by value, unless otherwise stated. In Java there is a garbage collector to return objects no longer referenced back to the heap. In C++, there is no such mechanism and any object dynamically allocated with new must be returned to the heap with delete, other wise a memory leak may occur. The main method in java returns void to the JVM. In C++ the main function return an int to the system.
4
A C++ program #include //the standard I/O libraries #include using namespace std; int main( int args, char** pszArgs[] ) { cout << “Hello World!” << endl; system( “pause” ); return 0; }
5
Using Dev-C++ If you have not downloaded Dev-C++, you can get the latest version from http://www.bloodshed.net/http://www.bloodshed.net/. Using Dev-C++ is very simple. Once you have started the program, simply go to File -> New -> Source File. This will open up a new blank file in which you can start typing your program into. Once you are done go to the menu item Execute->Compile. If there are no errors in your program a.exe file will be created, else, you will see the errors at the bottom of the screen. To run the program, go to Execute->Run.
6
Program explanation The first two lines of the program #include and #include are called pre-processor directives. Before the compiler runs, the pre-processor runs through and takes the action specified by the directives. The include directive basically tells the pre- processor to take the contents of the file in the angle brackets and paste it, in its place. The iostream file contains the functions required for I/O functions. The third line, using namespace std is very similar to the java import statement. Like the java package, similar classes or functions in C++ are placed into something called (namespace)s. However, the functionality between a C++ namespace and a java package, is not the same. All the standard libraries in C++ are placed under the namespace ‘std’. The using directive allows for the use of short names instead of the fully qualified long names, e.g.( cout instead of std::cout ). Line 5 is the declaration of the main function. In C++ the main function must return an integer. The arguments to the main function are the number of command line arguments passed to the program, and an array of strings that are the command-line arguments themselves. The 7 th line of this program prints the line “Hello World” to the monitor. The cout object is of type ostream, the class which contains all the functionally for output. Before the program exits the shell command “pause” is invoked to see the resulting output. The program then exits by returning 0 to the system.
7
Using C++ variable types #include using namespace std; int main( int args, char* pszArgs[] ) { int a = 3, b = 4; cout << "input number: "; cin >> a; cout << "input another: "; cin >> b; cout << "a+b: " << ( a + b ) << endl; string str; cout << "Input string: "; cin >> str; cout << str << endl; system("pause"); return 0; }
8
More C++ variable types #include using namespace std; int main( int args, char* pszArgs[] ) { int a = 3; int* ptrA = &a; //the int* says that this variable is a pointer to an int. A pointer in //in c++ is the same as a reference in java. It contains an address to //a variable in memory. To store the address of a variable in its // corresponding variable type use the ‘&’, “meaning address” of. To access //variable through the pointer, use the ‘*’, “meaning value pointed by”. cout << *ptrA << endl; int* ptrB = new int; //Create a new variable of type int. ptrB will point to it. *ptrB = 3; cout << *ptrB << endl; delete ptrB; //Must delete, or memory will not be freed!!!!!!!!!!!!!!!!!!! system("pause"); return 0; }
9
More C++ For an excellent tutorial on C++ go to this website: http://www.cplusplus.com/doc/tutorial/
10
The Java Native Interface To use a C++ function in Java code, the C++ function must be placed into a library. In the Windows Operating System, this would a dynamic link library or.dll. To create the DLL with Dev-C++, you need to create a DLL project. 1.Start Dev-C++. 2.Go to File->New->Project. 3. Click on the DLL project icon. 4.Type a name for the project in project name and push OK. 5.Choose a location to save the.dev project file. 6.Two new files will then open, dllmain.cpp and dll.h. Do not touch these files, just save them. 7.You then need to create the header file which contains the function signature of your native method. Simply run javah on your class file which will use the native method. 8.Once the header file is created, you need to add it to the DLL project. 9.Go to Project->Add to Project.
11
The Java Native Interface cont. 10.Find the header file and then click OK. 11.You then need to create a new file to put your native code into. 12.Go to Project->New File. 13.Choose a location to save it, then click OK. 14.In the new file, you need to include the header file using the #include pre-processor directive: #include. 15.Using the function signature that was made, you can start creating your native c++ function. 16.Once you are done, go to Execute->Rebuild All. If everything is ok, a.dll will be created which you can then use in your Java class. 17.In your Java class, before you use the native method, the DLL itself needs to be loaded with System.loadLibrary( “dll name”); 18.Only give the name of the library without the.dll extension. 19.If there was a problem, an UnstatisfiedLinkError will be thrown. 20.Once the library is loaded, you can use the native method like a regular java method.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.