Java-jni Java program to call C + + programs

Source: Internet
Author: User

Purpose: Write C + + functions and let Java call

Java code

1. Create Hellojni.java file, write code as follows->cmd javac Hellojni.java get hellojni.class file

public class Hellojni {public native void display ();//native declaration function is its language implementation static {//Represents a static statement block System.loadlibrary ("Hellodll") ;//means load dynamic-link library hellodll library}public static void main (string[] args) {new Hellojni (). display ();}}

2. Javah Hellojni get HelloJNI.h file in cmd

The following is a C + + program

1. Create Hellodll to compile the dynamic library project

2. Select DLL Empty Project

3. Add HelloDll.h, HelloDll.cpp

4. Copy the contents of the HelloJNI.h to the HelloDll.h file

5. Copy the jni.h, Jawt_md.h, jni_md.h three header files from the JDK that installs Java to the Hellodll project.

Jni.h Location

Jawt_md.h, jni_md.h location

Copy to Hellodll project file

6. Need to configure the bottom file path item Right-click Property->vc++ project, including directory-> /hellodll;

7. Implementing Functions in HelloDll.cpp

8. Generate the HelloDll.dll library file

Run the program

1. Place the HelloDll.dll library file in the Hellojni.class file directory

2. In cmd, Java hellojni

Java-jni Java program to call C + + programs

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.