java调用c++步骤
来源:互联网 发布:mac mini 换内存 编辑:程序博客网 时间:2024/06/12 01:34
- 定义java与c的接口
package com.test;
public class Test
{
public static native String test(int a);
}
public class Test
{
public static native String test(int a);
}
- 基于上步定义的接口,生成c的头文件
javah -classpath ./ -jni com.test.Test
生成c的头文件:com_test_Test.h
- 基于c的头文件,实现和封装
com_test_Test.cpp
#include "com_test_Test.h"
#include <stdlib.h>
#include <string.h>
jstring stoJstring(JNIEnv* env, const char* pat)
{
jclass strClass = env->FindClass("Ljava/lang/String;");
jmethodID ctorID = env->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V");
jbyteArray bytes = env->NewByteArray(strlen(pat));
env->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte*)pat);
jstring encoding = env->NewStringUTF("utf-8");
return (jstring)env->NewObject(strClass, ctorID, bytes, encoding);
}
jstring JNICALL Java_com_test_Test_test(JNIEnv * p, jclass c, jint a)
{
char buf[32] = {0};
sprintf(buf,"input is %d" , a);
return stoJstring(p , buf);
}
#include <stdlib.h>
#include <string.h>
jstring stoJstring(JNIEnv* env, const char* pat)
{
jclass strClass = env->FindClass("Ljava/lang/String;");
jmethodID ctorID = env->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V");
jbyteArray bytes = env->NewByteArray(strlen(pat));
env->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte*)pat);
jstring encoding = env->NewStringUTF("utf-8");
return (jstring)env->NewObject(strClass, ctorID, bytes, encoding);
}
jstring JNICALL Java_com_test_Test_test(JNIEnv * p, jclass c, jint a)
{
char buf[32] = {0};
sprintf(buf,"input is %d" , a);
return stoJstring(p , buf);
}
- 生成对应的c库
g++ -fPIC -D_REENTRANT -I/usr/lib/jvm/java-1.5.0-sun-1.5.0.19/include -I/usr/lib/jvm/java-1.5.0-sun-1.5.0.19/include/linux -c com_test_Test.cpp
g++ -shared com_test_Test.o -o libtest.so
- 设置环境变量
export LD_LIBRARY_PATH=/root/test
- 编写测试文件
package com.test;
public class Main
{
public static void main(String args [])
{
System.loadLibrary("test");
System.out.println("output:" + Test.test(100));
}
}
public class Main
{
public static void main(String args [])
{
System.loadLibrary("test");
System.out.println("output:" + Test.test(100));
}
}
- 运行测试
java -Djava.library.path=. -jar Test.jar
java -Djava.library.path=. com.test.Test
- 在JAVA中调用C步骤
- JNI java调用c函数 native 步骤 代码
- 最简单的Java调用C/C++代码的步骤
- 从 Java 代码调用 C/C++ 的六个步骤
- 在c/c++用调用java代码步骤
- Java调用dll步骤
- java调用c++步骤
- python调用C语言步骤
- java中调用c(c++)写的dll文件的实现及步骤
- java中调用c(c++)写的dll文件的实现及步骤
- java中调用c(c++)写的dll文件的实现及步骤
- java中调用c(c++)写的dll文件的实现及步骤(转)
- java中调用c(c++)写的dll文件的实现及步骤(转)
- java中调用c(c++)写的dll文件的实现及步骤
- java中调用c(c++)写的dll文件的实现及步骤
- java中调用c(c++)写的dll文件的实现及步骤
- Java调用dll步骤1
- 建立Android SDK下的JNI、JAVA应用完整步骤---Android JAVA调用C+
- MSVCP100.dll 丢失的问题
- 表格解述 HTTP状态码
- jQuery中常用的函数方法
- Xcode 4.6.2 运行次数为偶数程序崩溃问题
- 由“Jasperrpeorts 4.1.2升级到5.1.2对flex项目的解析”到AS3 带命名空间的XML的操作
- java调用c++步骤
- Mschart 使用方法
- 深入理解JavaScript系列(41):设计模式之模板方法
- java配置
- IOS中关于NSTimer使用知多少
- 深入理解JavaScript系列(42):设计模式之原型模式
- 精通C#(第6版)
- 深入理解JavaScript系列(43):设计模式之状态模式
- 很牛的vim使用技巧