Java Jni call and write dll detailed tutorial

Source: Internet
Author: User

To use JNI, you need to generate the header file of the JAVA call convention for C \ C ++ and modify the source code of the standard windows DLL. The following is a simple example to demonstrate the entire process.
Environment: win7 64-bit OS, JDK1.7 64-bit, vs2010 (supports x64 compilation ).

I. native declaration class

 

The code is as follows: Copy code
Package com. ninecmd;
 
Public class MyClass {
Static
{
System. loadLibrary ("jnidll ");
}
Public native static void SayHello (String word );
Public native static String RepeatWord (String word );
/* Public static void main (String [] args)
{
SayHello ("hello ");
String str = RepeatWord (" ");
System. out. println (str );
}
*/
}

"SayHello" and "RepeatWord" are function declarations in future dll files. This class is used to generate C \ C ++ header files.
II. Generate the C \ C ++ header file
Note the classpath path. In this example, the complete path of the MyClass class is E: \ src \ com \ ninecmd \ MyClass. java. The class package name is com. ninecmd, so the classpath should be set to E: \ src.

Switch cmd to E: \ src.
1. Compile MyClass and generate MyClass. class in the same directory

The code is as follows: Copy code

 
E: \ src> javac com \ ninecmd \ MyClass. java

2. Generate the header file (note the classpath path settings)

The code is as follows: Copy code

 
E: \ src> javah-classpath E: \ src-d com \ ninecmd \-jni com. ninecmd. MyClass

Javah parameter description:

 
Javah-classpath class Directory-d header file output Directory-jni complete class name

The com_ninecmd_MyClass.h file is generated in the E: \ src \ com \ ninecmd directory.

The code is as follows: Copy code
 
/* Do not edit this file-it is machine generated */
# Include <jni. h>
/* Header for class com_ninecmd_MyClass */
 
# Ifndef _ Included_com_ninecmd_MyClass
# Define _ Included_com_ninecmd_MyClass
# Ifdef _ cplusplus
Extern "C "{
# Endif
/*
* Class: com_ninecmd_MyClass
* Method: SayHello
* Signature: (Ljava/lang/String;) V
*/
JNIEXPORT void JNICALL Java_com_ninecmd_MyClass_SayHello
(JNIEnv *, jobject, jstring );
 
/*
* Class: com_ninecmd_MyClass
* Method: RepeatWord
* Signature: (Ljava/lang/String;) Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_ninecmd_MyClass_RepeatWord
(JNIEnv *, jobject, jstring );
 
# Ifdef _ cplusplus
}
# Endif
# Endif

3. Write DLL code
1. In this example, write x64 DLL, open vs2010, and create an mfc dll project. Change platform to x64.


2. Introduce the header file. To make it easy, I changed the header file to MyClass. h.
3. Compile the export function consistent with the header file declaration.
Main code:

 

The code is as follows: Copy code

// Convert the jstring type to the windows char * type
Char * jstringToChars (JNIEnv * env, jstring jstr)
{
Int length = (env)-> GetStringLength (jstr );
Const jchar * jcstr = (env)-> GetStringChars (jstr, 0 );
Char * rtn = (char *) malloc (length * 2 + 1 );
Int size = 0;
Size = WideCharToMultiByte (CP_ACP, 0, (LPCWSTR) jcstr, length, rtn, (length * 2 + 1), NULL, NULL );
If (size <= 0)
Return NULL;
(Env)-> ReleaseStringChars (jstr, jcstr );
Rtn [size] = 0;
Return rtn;
}
 
// Convert windows char * type to jstring type
Jstring CharsTojstring (JNIEnv * env, char * str)
{
Jstring rtn = 0;
Int slen = strlen (str );
Unsigned short * buffer = 0;
If (slen = 0)
Rtn = (env)-> NewStringUTF (str );
Else
    {
Int length = MultiByteToWideChar (CP_ACP, 0, (LPCSTR) str, slen, NULL, 0 );
Buffer = (unsigned short *) malloc (length * 2 + 1 );
If (MultiByteToWideChar (CP_ACP, 0, (LPCSTR) str, slen, (LPWSTR) buffer, length)> 0)
Rtn = (env)-> NewString (jchar *) buffer, length );
    }
If (buffer)
Free (buffer );
Return rtn;
}
 
JNIEXPORT void JNICALL Java_com_ninecmd_MyClass_SayHello
(JNIEnv * env, jobject obj, jstring jstr)
{
Char * str = jstringToChars (env, jstr );
CString out (str );
AfxMessageBox (out ));
}
 
JNIEXPORT jstring JNICALL Java_com_ninecmd_MyClass_RepeatWord
(JNIEnv * env, jobject obj, jstring jstr)
{
Return jstr;
}

4. Compile and release the dll.
4. JNI calls dll
Copy the generated dll file named jnidll. dll to the System32 directory. Remove the main function comment of the class code in step 1. Recompile the class (to ensure compatibility with Chinese strings, the code file must be ANSI encoded) and run the test.
Or use eclipse to test
Test successful!

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.