Create a DLL file instance that can be called by Java
-----------------------------------------------------------
1. Compile a class file
Package com. Xinfeng. INTF;
Public class animal {
Public native int getage ();
Public native void setage (INT age );
Public native void say ();
Public native void run ();
}
Methods implemented in other languages modified with the native keyword
2. Use the javah command to generate a. h file.
A. Compile the animal. Java file into a class file.
B. Set classpath = "X: \ testinvokedll \ bin". The class file is generated in X: \ testinvokedll \ bin \ com \ Xinfeng \ INTF \ animal. Class.
C. Go to the X: \ testinvokedll \ bin directory.
D. javah com. Xinfeng. INTF. Animal
The current directory contains a com_xinfeng_intf_animal.h file.
----------------------------------------------------------------------------
/* Do not edit this file-it is machine generated */
# Include <JNI. h>
/* Header for class com_xinfeng_intf_animal */
# Ifndef _ included_com_xinfeng_intf_animal
# DEFINE _ included_com_xinfeng_intf_animal
# Ifdef _ cplusplus
Extern "C "{
# Endif
/*
* Class: com_xinfeng_intf_animal
* Method: getage
* Signature: () I
*/
Jniexport jint jnicall java_com_xinfeng_intf_animal_getage
(Jnienv *, jobject );
/*
* Class: com_xinfeng_intf_animal
* Method: setage
* Signature: (I) V
*/
Jniexport void jnicall java_com_xinfeng_intf_animal_setage
(Jnienv *, jobject, jint );
/*
* Class: com_xinfeng_intf_animal
* Method: Say
* Signature: () V
*/
Jniexport void jnicall java_com_xinfeng_intf_animal_say
(Jnienv *, jobject );
/*
* Class: com_xinfeng_intf_animal
* Method: Run
* Signature: () V
*/
Jniexport void jnicall java_com_xinfeng_intf_animal_run
(Jnienv *, jobject );
# Ifdef _ cplusplus
}
# Endif
# Endif
----------------------------------------------------------------------------
3. Generate the DLL file. Here, the dev C ++ V5.0 Chinese version is used as an example.
A. Create a DLL project named testdll.
B. Remove the two files.
C. Right-click the project name, add it, and select com_xinfeng_intf_animal.h.
D. Create a new unit. If the name is testdll. cpp, implement the method declared by com_xinfeng_intf_animal.h.
----------------------------------------------------------------------------
# Include <stdio. h>
# Include "com_xinfeng_intf_animal.h"
Int age = 0;
Jniexport jint jnicall java_com_xinfeng_intf_animal_getage
(Jnienv *, jobject)
{
Return age;
}
Jniexport void jnicall java_com_xinfeng_intf_animal_setage
(Jnienv *, jobject, jint _ age)
{
Age = _ age;
}
Jniexport void jnicall java_com_xinfeng_intf_animal_say
(Jnienv *, jobject)
{
Printf ("Hello world! \ N ");
}
Jniexport void jnicall java_com_xinfeng_intf_animal_run
(Jnienv *, jobject)
{
Printf ("running! \ N ");
}
----------------------------------------------------------------------------
E. Click "run"-> "compile". Then you will see many errors that are not declared for data types, because
Com_xinfeng_intf_animal.h uses many Custom Data Types of java_home/include/JNI. h file, and JNI. h introduces
Java_home/include/Win32/jni_md.h is used. In this case, you need to introduce JNI. h and jni_md.h to the project and left-click the project name.
"Project Properties"-> "file/directory"-> "include file directory"
Add the java_home/include/Win32 folder. After confirmation, compile again.
Find the testdll. dll file, and the DLL file is generated.
4. Call the DLL file
A. Copy the testdll. dll file under the project.
B. Create a Java File
----------------------------------------------------------------------
Package com. Xinfeng. Main;
Import com. Xinfeng. INTF. animal;
Public class main {
Static {
/*
* Transfer the DLL file without a suffix.
* Different system library file extensions are different.
* For example, if Windows is a DLL, but Linux is a so
* JVM loads the corresponding library files in different systems.
*/
System. loadlibrary ("delltemplete ");//
}
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Animal persion = new animal ();
Persion. setage (23 );
System. Out. println ("Age:" + persion. getage ());
Persion. Say ();
Persion. Run ();
}
}
----------------------------------------------------------------------
Some of the above terms may have been used incorrectly. Please include more!