How to use Java to call C Programs through eclipse development in Linux

Source: Internet
Author: User

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!

Related Article

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.