Java JNI calls an unmanaged DLL

Source: Internet
Author: User

Step 1:

Compile Java class, e.g .:

 
// Filename: helloworld. javapublic class helloworld {public native void displayhelloworld (); Public native int add (int A, int B); static {system. loadlibrary ("hello");} public static void main (string [] ARGs) {helloworld hW = new helloworld (); HW. displayhelloworld (); system. out. println (); system. out. println ("1 + 20 =" + HW. add (1, 20 ));}}

Step 2: javac helloworld. Java

Step 3: javah helloworld

The generated header file is as follows:

/* Do not edit this file-it is machine generated */# include <JNI. h>/* Header for class helloworld */# ifndef _ included_helloworld # DEFINE _ included_helloworld # ifdef _ cplusplusextern "C" {# endif/** class: helloworld * method: displayhelloworld * Signature: () V */jniexport void jnicall java_helloworld_displayhelloworld (jnienv *, jobject);/** class: helloworld * method: Add * Signature: (ii) I */jniexport jint jnicall java_helloworld_add (jnienv *, jobject, jint, jint); # ifdef _ cplusplus} # endif

Step 4: Use vc6 to create a Win32 DLL project named hello

Step 5:

Tools-> Option-> directory (Label)

Directories adds two include paths (For details, refer to the actual include paths of Java ):

D: \ Java \ JDK \ jdk1.6.0 _ 21 \ include

D: \ Java \ JDK \ jdk1.6.0 _ 21 \ include \ Win32

Step 6:

Create hello. cpp

 # include 
  
    # include "helloworld. H "# include 
   
     # include 
    
      jniexport void jnicall java_helloworld_displayhelloworld (jnienv *, jobject) {printf ("Hello China! @ # $ ");}/** Class: helloworld * method: Add * Signature: (ii) I */jniexport jint jnicall java_helloworld_add (jnienv *, jobject, jint, jint B) {return a + B ;}
    
   
  

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.