Use of Java Native interface

Source: Internet
Author: User

Classroom teacher said, native interface can achieve the mixed language, to make up for the deficiencies of the Java language, hear here I am overjoyed, I thought I can use my C + +, haha, happy, joy, I returned to the dormitory at night to try this native interface function.

First find the following code from the Internet:

1. Java is compiled first as a. class file:

public class HelloWorld {public native void Displayhelloworld (); static {system.loadlibrary (' HelloWorld ');} public stat IC void Main (string[] args) {new HelloWorld (). Displayhelloworld ();}}

2. Suppose the Helloworld.class directory is F:/javacode/native/bin/helloworld.class

Then enter Javah-classpath-jni F:/javacode/native/bin/helloworld in the root directory via cmd

Can output HelloWorld.h header file, the following to use, Haha, good custody;

3. Here are some HelloWorld.cpp files, I like to use the VC6.0 here, the establishment of the dynamic link library file, not the console oh, careful,

Content is:

#include <stdio.h>
#include <jni.h>
#include "HelloWorld.h"

Jniexport void Jnicall
Java_helloworld_displayhelloworld (jnienv *env, Jobject obj)
{
printf ("Hello world!/n");
Return
}

The jni.h here is when the JDK is installed in its root directory, assuming that you are the default installation under the C:/Program Files/java/jdk1.6.0_10/include folder, through the

The Directories tab in Tools->options adds the directory where the JNI resides, as shown in the screenshot below.

4. After compiling, execute can get our dream of HelloWorld.dll file, hey, hehe ready to work here even finished, right, but also to the HelloWorld.dll file

Copy to the same folder as the HelloWorld.dll file, and then enter the Java HelloWorld into its folder by entering the command prompt, cmd

Haha, see.  Hello world! The first Java and C mixed program in your life was born, happy, haha, enjoy the charm of language.

Related Article

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.