Java calls the so file generated by C/C ++ in linux

Source: Internet
Author: User

Java calls the so file generated by C/C ++ in linux
1.
CplusUtil. java is a tool class in java web engineering.
The content is as follows:
CplusUtil. java

package cn.undoner.utils;/** * Created by ${undoner<a data-cke-saved-href="mailto:[email protected]" href="mailto:[email protected]"></a>} on 16-2-25. */public class CplusUtil {    public native void sayHello();    static{        System.out.println(System.getProperty("java.library.path"));        System.load("/usr/lib/jvm/java/jre/lib/amd64/server/MyJni.so");    }    public static void main(String[] args){        CplusUtil h = new CplusUtil();        h.sayHello();    }}

2.
You can use the IDE tool (Eclipse, intellij, javac, and so on) to compile the entire project and generate the corresponding class file.
The path of this article is:
/Project name/target/classes/cn/undoner/utils/CplusUtil. class


3. Generate the. h header file for the c function to be called by utils.
Note: When the package name is available, remember to include the package name path when generating the. h header file.
Javah-classpath classes-jni cn. undoner. utils. CplusUtil


File generated: cn_undoner_utils_CplusUtil.h
The content is as follows:
/* DO NOT EDIT THIS FILE - it is machine generated */#include 
 
  /* Header for class cn_undoner_utils_CplusUtil */#ifndef _Included_cn_undoner_utils_CplusUtil#define _Included_cn_undoner_utils_CplusUtil#ifdef __cplusplusextern "C" {#endif/* * Class:     cn_undoner_utils_CplusUtil * Method:    sayHello * Signature: ()V */JNIEXPORT void JNICALL Java_cn_undoner_utils_CplusUtil_sayHello  (JNIEnv *, jobject);#ifdef __cplusplus}#endif#endif
 

4. Compile c Functions
File: MyJni. c
The content is as follows:
#include <jni.h>#include "cn_undoner_utils_CplusUtil.h"#include <stdio.h>JNIEXPORT void JNICALL Java_cn_undoner_utils_CplusUtil_sayHello(JNIEnv *env,jobject obj){    printf("Hello JNI");    return;}</stdio.h></jni.h>

5.
Compile the. c file into. o, and then convert it into a. so or. dll file.
Gcc-fPIC-D_REENTRANT-I/usr/lib/jvm/java/include/linux-c MyJni. c
Generate file: MyJni. o

6.
Compile the. o file into a. so file.
Gcc-shared MyJni. o-o MyJni. so
Generate file: MyJni. so

7.
The path of MyJni. so in this article is:

/Usr/lib/jvm/java/jre/lib/amd64/server/MyJni. so

8.
Execute CplusUtil. class
Run CplusUtil in ide or run the following java command:
Java cn. undoner. utils. CplusUtil

9.
Result:
/Usr/lib/jvm/java/jre/lib/amd64/server:/usr/lib/jvm/java/jre/lib/amd64: /usr/lib/jvm/java/jre /.. /lib/amd64:/home/vobile/java_tool/idea-IU-141.178.9/bin:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib: /usr/lib
Hello JNI

Note: System. getProperty ("java. library. path") outputs the following information:
/Usr/lib/jvm/java/jre/lib/amd64/server:/usr/lib/jvm/java/jre/lib/amd64: /usr/lib/jvm/java/jre /.. /lib/amd64:/home/vobile/java_tool/idea-IU-141.178.9/bin:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib: /usr/lib

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.