Java local call (JNI) using Dev C ++ for simple implementation

Source: Internet
Author: User

In my recent project, I need to use JNI to call C/C ++ compiled programs. Since I was not familiar with JNI, I found many of them on the Internet are implemented based on the VC or vs2008 series. In this way, it is very troublesome to install hundreds of megabytes of VC or several GB of vs for a simple call, because I have been using Dev C ++ for C Development (this Dev is very compact and powerful, but it is unfriendly to auto-completion ), so I decided to use Dev to compile the local JNI file: the steps are as follows:

1.0 write Java classes

public class My{  public native String hello(String arg);  static{    System.loadLibrary("My");} public static void main(String[] args){      new My().hello("Hello");}}

2.0 compile the Java file: javac-CP my. Java

3.0 generate the header file: javah-JNI my will generate the header file of my. h In the Directory

//My.h/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class My */#ifndef _Included_My#define _Included_My#ifdef __cplusplusextern "C" {#endif/* * Class:     My * Method:    hello * Signature: (Ljava/lang/String;)Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_My_hello  (JNIEnv *, jobject, jstring);#ifdef __cplusplus}#endif#endif

4.0 open Dev C ++ to create a project, select DLL on the basic tab, enter my as the project name, and delete the default files generated by Dev.

5.0 copy the generated my. h file to my project, right-click the project, and create a new my. cpp file to implement my. h

// My. CPP # include <windows. h> # include <stdlib. h> # include <stdio. h> # include "My. H "/** class: My * method: HELLO * Signature: (ljava/lang/string;) ljava/lang/string; */jniexport jstring jnicall java_my_hello (jnienv * ENV, jobject OBJ, jstring pstring) {// obtain the const char * nativestring = env-> getstringutfchars (pstring, 0); printf ("% s ", nativestring); // the dialog box MessageBox (0, text (native String), "Hi", mb_iconinformation); // don't forget this line !!! Env-> releasestringutfchars (pstring, nativestring); Return pstring ;}

At this time, the above files will be compiled with an error: JNI. h cannot be found. Don't worry, I will tell you how to solve it:

Right-click the dev my project, select project properties ---- file/directory ---- contains file directory --- add c: \ Program Files \ Java \ jdk1.6.0 _ 10 \ include and C: \ Program Files \ Java \ jdk1.6.0 _ 10 \ include \ Win32 [JDK directory varies depending on your installation directory]

If no syntax error occurs during compilation, the following error occurs: My. dll.

6.0 copy my. DLL to the directory where the my. Class file is located

Java my

The dialog box "hello" and "window" appears:

It is best to add a stdafx. h file to include this file in my. cpp:

// Stdafx. h: Include files in the standard system, // or frequently used but infrequently changed // project-specific include files // # pragma once // if the platform located before the platform specified below must be used as the target, modify the following definitions. // For the latest information about the corresponding values of different platforms, refer to msdn. # Ifndef winver // allows you to use features specific to Windows XP or later. # Define winver 0x0501 // change this value to the appropriate value for other Windows versions. # Endif # ifndef _ win32_winnt // allows you to use features specific to Windows XP or later. # DEFINE _ win32_winnt 0x0501 // change the value to the corresponding value for other Windows versions. # Endif # ifndef _ win32_windows // allows you to use features specific to Windows 98 or later. # DEFINE _ win32_windows 0x0410 // change this value to an appropriate value to specify Windows ME or a later version as the target. # Endif # ifndef _ win32_ie // allows you to use features specific to IE 6.0 or later. # DEFINE _ win32_ie 0x0600 // change the value to the corresponding value to apply to other IE versions. # Endif # define win32_lean_and_mean // exclude rarely used files from the Windows header // windows header file: # include <windows. h> # include <stdlib. h> # include <stdio. h> // todo: reference other header files required by the program here

Dev C ++ downloading JNI here will cause Java to lose the cross-platform function, so unless necessary, you should try to use Java to implement the functions in the DLL file!

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.