test.java
public class test {
static
{
//System.out.println(System.getProperty("java.library.path"));
String str = System.getProperty("java.library.path");
System.loadLibrary("test");
}
public native static void HelloWorld();
void Start()
{
HelloWorld();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
test xTest = new test();
xTest.Start();
}
}
test.cpp test.h
// 头文件
//
#ifndef _BE_CALL_H_
#define _BE_CALL_H_
// 包含相应的头文件
#ifdef WIN32
#include "jni.h"
#else
//#include </usr/lib/jvm/jdk1.7.0_79/include/linux/jni_md.h>
//#include </usr/lib/jvm/jdk1.7.0_79/include/jni.h>
#include "inc/java/jni.h"
#include "inc/java/jni_md.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT void JNICALL Java_test_HelloWorld(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
#include "test.h"
JNIEXPORT void JNICALL Java_test_HelloWorld(JNIEnv *, jclass)
{
printf("hello world!\n");
}
makefile
CXX = g++
CXXFLAG =
C11 = -std=c++11
CUR_DIR = ./
OBJ = \
DLL_OBJ = test.o
DEL_FILE = rm -f
libtest.so:$(OBJ) $(DLL_OBJ)
$(CXX) -fPIC -shared -o libtest.so $(OBJ) $(DLL_OBJ) -lpthread \
clean:
$(DEL_FILE) *.o
test.o: $(CUR_DIR)/test.cpp $(CUR_DIR)/test.h \
$(CUR_DIR)/inc/java/jni.h \
$(CUR_DIR)/inc/java/jni_md.h \
$(CXX) -std=c++11 -fPIC -c $(CXXFLAGS) $(INCPATH) -o test.o $(CUR_DIR)/test.cpp