Java JNI full instance from the beginning

Source: Internet
Author: User

Java Call DLL

Preparation stage: 1). Java Environment JDK (in fact this ...)

2). MyEclipse or eclipse (this is your own choice)

3). C language IDE Microsoft Visual c++6.0 (This can also be your own choice)

The tools used in the following steps are used in my own case

First step: Open Eclipse Build Project: generate a communication contract interface between Java and C

Step two: Find the Eclipse directory space where the project is located, view in properties (Eclipse presses shortcut key Alt+enter)

Then open cmd to switch directories to the Helloworld.java directory, for example, my directory is "E:\workspace\JavadllDemo\src\" and then use the Javah name to generate the header file to be used when C is compiled:

Step three: Write our DLL file, open Microsoft Visual c++6.0

After the establishment, in the E:\WORKSPACE\JAVADLLDEMO\SRC directory just below the generated HelloWorld.h file copied to the C workspace, However, because the compiler can be called by the Java program can also be called by the C program DLL time also need two header files, one is jni.h one is jni_md.h

These two files are located in the Include folder in your JDK installation directory java_home\jdk1.6.0_17\include\jni.h, java_home\jdk1.6.0_17\include\win32\jni_md.h

To copy these two files and HelloWorld.h altogether three files into the workspace of the C project, and after copying to the C project workspace, you need to change the HelloWorld header file Introduction method to change # include <jni.h> to #include "Jni.h" finally:

Fourth step: Write the DLL inside the function to be called by Java

HelloWorld.cpp:Defines the entry point for the DLL application.//#include "HelloWorld.h"/* * Class:     HelloWorld * Method:    init * Signature: (i) I */jniexport jint jnicall java_helloworld_init (jnienv *env, Jobject job, Jint Lport) {in T number=10; Number+=lport; return number;} /* Class:     HelloWorld * Method:    print * Signature: (ljava/lang/string;) V */jniexport void Jnicall java_helloworl D_print (jnienv *env, Jobject job, jstring str) {printf ("%d\n", str);}

As shown in the following:

The final build compiles our DLLs (first reorganized, compiled) such as:

This error may occur during the compilation process: Unexpected end of file while looking for precompiled header directive workaround such as:

As set after the compilation, if the prompt is not wrong, it means that the compilation is successful!

After compiling, we will find HelloWorld.dll in our C Project Workspace Debug folder, when we copy this HelloWorld.dll to the Lib file in our Java project.

Java JNI full instance from the beginning

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.