A simple example of the interface of a dynamically connected library DLL file that is invoked in Java by calling C + + written __c++

Source: Internet
Author: User

1, first in Java to write a simple class

public class Hello
{public
	native void SayHello ();
	static {
		system.loadlibrary ("Hellodll");
	}
	public static void Main (string[] args) {
    	new Hello (). SayHello ();
    }
}

Compiling the class using Javac Hello.java

Then use Javah hello to generate the Hello.h header file

2. Create a new dynamic link library project in Visual C + + 6.0, project named Hellodll

3. In the project, a new header file named: hello.h, copy the code in the Hello.h file just generated, as follows:

/* Do isn't EDIT this file-it is machine generated/*
#include <jni.h>/
* Header for class Hello/*/

#i Fndef _included_hello
#define _included_hello
#ifdef __cplusplus
extern "C" {
#endif/
*
 * Class:     Hello
 * Method:    SayHello
 * Signature: () V
 /
jniexport void Jnicall Java_hello_sayhello
  (jnienv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

No need to make any changes

At this point, create a new Test.cpp file to implement the function of the header file just now, the code is as follows

#include "Hello.h"
#include <jni.h>

jniexport void Jnicall Java_hello_sayhello
(jnienv *, Jobject) {
	printf ("Hello world,i ' m coming!");
	return;
}

At this point if the compilation will be an error, said that the jni.h file can not be found, as follows:

Fatal error C1083:cannot open include file: ' jni.h ': No such file or directory

Only step 4 can be done at this point

4. Copy the Jni_md.h files under Jni.h and ~\jdk1.6.0_25\include\win32 to Visual C + + 6.0 in the JDK installation directory ~\jdk1.6.0_25\include D:\Microsoft In the Visual studio\vc98\include directory

5, at this time in the compilation of linked Test.cpp files, you can generate dynamic link library files:

Then copy the file to the same directory as the Hello.class file, run the Java file, you can invoke the interface of C/E + + writing implementation, that is, dynamic link library file

The results of the operation are as follows:

This calls success.





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.