In Linux, the method of Calling C program using Java through eclipse development is as follows:
1. Set up a Java project and create a Java file as follows:
Public class testso {
Static {
System. loadlibrary ("testc"); // The project is displayed as libtestc. So. You only need to enter the actual name,
// You can leave the prefix suffix empty.
}
Public native static int get ();
Public native static void set (int I );
Public static void main (string [] ARGs ){
Testso T = new testso ();
T. Set (10 );
System. Out. println (T. Get ());
}
}
2. Compile the testso. Java file (javac testso. Java) on the terminal or using eclipse to generate the corresponding class file testso. Class.
3. Similarly, use the javah testso. Class file to generate the testso. h file. The. h file content is as follows:
/* Do not edit this file-it is machine generated */
# Include <JNI. h>
# Ifndef _ testso __
# DEFINE _ testso __
# Ifdef _ cplusplus
Extern "C"
{
# Endif
Jniexport jint jnicall java_testso_get (jnienv * ENV, jclass );
Jniexport void jnicall java_testso_set (jnienv * ENV, jclass, jint );
# Ifdef _ cplusplus
}
# Endif
# Endif/* _ testso __*/
4. Create a C Project. Note that the project type is shared library. then create a c file with the following content:
# Include "testso. H"
Int I = 0;
Jniexport jint jnicall java_testso_get (jnienv * ENV, jclass JC ){
Return I;
}
Jniexport void jnicall java_testso_set (jnienv * ENV, jclass JC, jint J ){
I = J;
}
5. Copy the test. h file to the C Project!
6. When running testso. Java, you need to add a Good Luck line environment (note that variable cannot be entered as needed !) :
Click RUN ---> in environment, click New ---> variable fill in: LD_LIBRARY_PATH -----> value fill in: directory of the so file, which is/home/Alen/workspace/testc/debug
7. Run the Java file. 10 is displayed! Congratulations! Other similar methods for Calling C are also implemented. Welcome to discuss them together!