Reference:
https://youtu.be/4B-cNXYNRDo
Step 1: prepare files (printmsg.c & main.java)
cat printmsg.c
#include<stdio.h>
void printmsg()
{
printf("hello there world from Gang\n");
}
cat main.java
class MyMain{
public native void printmsg();
static
{
System.loadLibrary("prtmsg");
}
public static void main(String[] args){
MyMain hello = new MyMain();
hello.printmsg();
}
}
Step 2: compileing caller (JAVA)
javac main.java # output MyMain.class
javah MyMain #output MyMain.h
cat MyMain.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class MyMain */
#ifndef _Included_MyMain
#define _Included_MyMain
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: MyMain
* Method: printmsg
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_MyMain_printmsg
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
Step 3: re-editing callee (C)
#edit printmsg.c to include "MyMain.h" & modify function signature
#include<stdio.h>
#include "MyMain.h"
//void printmsg(){
JNIEXPORT void JNICALL Java_MyMain_printmsg
(JNIEnv *env, jobject obj)
{
printf("hello there world from Gang\n");
}
Step 4: compiling callee (C)
gcc -shared -o libprtmsg.so printmessage.c -fPIC -I/usr/lib/jvm/java-1.8.0-openjdk-amd64/include -I/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/linux
Step 5: preparing environment & testing:
cat build.sh
export LD_LIBRARY_PATH=`pwd`
echo $LD_LIBRARY_PATH
unset LD_LIBRARY_PATH
java MyMain
'via Blog this'
Blog Archive
-
▼
2017
(115)
-
▼
December
(22)
- alumae/kaldi-gstreamer-server: Real-time full-dupl...
- How to Auto Execute Commands/Scripts During Reboot...
- 什么是人才
- 工作区和暂存区 - 廖雪峰的官方网站
- 8. Commiting the changes
- [c++][study notes]Polymorphism in C++
- WebRTC中的Opus编码揭秘 - CSDN博客
- Vi Cheat Sheet
- 8. Commiting the changes
- 买人寿保险Life Insurance省钱的小trick,如何拿到commission rebate...
- [Git高级教程(二)] 远程仓库版本回退方法 - CSDN博客
- Kaldi: Kaldi logging and error-reporting
- Java Native Interface (JNI) for calling C-libraries
- java - How to convert jbyteArray to native char* i...
- [c++ note] Differences using char* and std::string...
- C++ md5 function :: zedwood.com
- [studying note] Sample JNI/C++ HelloWorld
- merge - Finding and merging down intervalls in Per...
- SGE: Monitoring and Managing Your Job
- Plotting multiple histograms in one figure - MATLA...
- 为什么你应该关注Amazon SageMaker
- 声纹识别在智能家居领域的应用前景_物联网在线
-
▼
December
(22)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment