Java JNI Development

Source: Internet
Author: User

The general steps for JNI program development are as follows:

L compile the call class in Java

L use javah to generate header files for C/C ++ native functions

L call other functions required in C/C ++ to implement native functions (in principle, any resource can be called)

L add all native libraries and resources dependent on the project to Java. Library. Path of the Java Project

L generate a Java program

L release Java applications and DLL Libraries

 

 

 

Example of JNI program development:

1. Create a New testhello. Java in the Eclipse project and enter the following content:

[Java]View plaincopy
  1. Public class testhello {
  2. Static {
  3. System. loadlibrary ("testhello ");
  4. }
  5. Public static native void Hello (string MSG );
  6. Public static void main (string [] ARGs ){
  7. Hello ("Hello, kimm! ");
  8. }
  9. }

 

 

Compile and generate the testhello. Class file.

2. Use the javah testhello command in the command line to generate the header file testhello. H (that is, the interface of JNI proxy stub ).

3. Create a new project testhello in vc6. The project type is Win32 dynamic-Link Library. Click OK.

 

In the displayed window, select a simple DLL project and click Finish.

 

 

 

 

 

 

 

Open the file directory of the project and copy the testhello. h file generated in step 2 to this directory. Click FileView in the middle of the Left to switch to the file browsing mode. Right-click header files and choose add files to folder ....

Select the testhello. h file and click OK.

 

 

 

Open the stdafx. h file and add the following content at the end:

# Include <JNI. h>

# Include "testhello. H"

 

 

 

Open the testhello. cpp file and add a code at the end:

Jniexport void jnicall java_testhello_hello (jnienv * ENV, jclass OBJ, jstring jmsg)

{

Const char * strmsgptr = env-> getstringutfchars (jmsg, 0 );

 

MessageBox (0, strmsgptr, "message box from VC ++", 0 );

 

Env-> releasestringutfchars (jmsg, strmsgptr );

 

}

 

 

 

 

On the VC menu, select Tools-OPTIONS ..., Open the option dialog box. In the directories folder, add the include and include/Win32 folders under the JDK folder.

 

 

 

Click build-build all in the menu item on VC to generate testhello. dll.

4. Copy testhello. dll in the debug folder of the VC project to the folder where testhello. Class is located.

5. Enter Java testhello in the command line to bring up the MessageBox dialog box. Win32 API called successfully.

 

 

 

 

 

 

Next, set the JVM search path so that it can find your DLL. The JVM search path is determined by the system attribute of Java. Library. path. The default value is the path content in the system environment variable. Therefore, you can modify the PATH variable to set Java. library. path attribute (eclipse needs to be restarted after the change). The general method is to add the absolute path of the DLL folder to the path. Another method is to add "-djava. Library. Path = absolute path of the DLL folder" to the parameters of the Java command to set (you can use; Separate multiple paths ). For applications in the eclipse development environment, you can modify the startup parameters in the VM
Add the preceding parameters to the arguments editing box. You can edit application.ini in the startup directory of the eclipseinstallation package, and add the preceding parameters after-vmargs to set the value of Java. Library. Path. It should be noted that once the JVM has been started, the content of Java. Library. path cannot be modified, that is, through:

 

System. setproperty ("Java. Library. Path", "C:/mylib ");

 

This method cannot achieve the goal, because the attribute is read-only. Sun's forum once discussed how to modify java. Library. path in the Code. The conclusion is: The Code cannot be modified! If "Java-djava. library. path = C:/mylib "is too hard to write. It can only be used to pre-process the path through methods such as shell programming to improve its flexibility.

 

If your DLL is encapsulated in a jar package, first extract it to a temporary path, and then add the path to djava. library. or decompress it to the system path.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.