我正在處理一些本地代碼以將RPI感應帽與我的java東西進行接口,而且我無法讓我的本機意味着。我已經在java中編寫了存根,編譯它然後使用javah來提取頭文件。我創建了C中的方法來將一個簡單的char數組轉換爲一個字符串來返回。我似乎無法得到它編譯。 的Java:無法編譯JNI C不兼容的指針類型
/**
* NativeTest - PACKAGE_NAME
* Created by matthew on 21/07/16.
*/
class SenseHat
{
static {
System.loadLibrary("SenseHat");
}
public native String getTemperature();
public native String getHumidity();
public native String getOrientation();
}
頭文件
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class SenseHat */
#ifndef _Included_SenseHat
#define _Included_SenseHat
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: SenseHat
* Method: getTemperature
* Signature:()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_SenseHat_getTemperature
(JNIEnv *, jobject);
/*
* Class: SenseHat
* Method: getHumidity
* Signature:()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_SenseHat_getHumidity
(JNIEnv *, jobject);
/*
* Class: SenseHat
* Method: getOrientation
* Signature:()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_SenseHat_getOrientation
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
C文件:
#include <jni.h>
#include <stdio.h>
#include "SenseHat.h"
JNIEXPORT jstring JNICALL Java_SenseHat_getTemperature(JNIEnv *env, jobject thisObj) {
char done[] = "temperature";
jstring answer;
/* Make a new String based on done, then free done. */
answer = (*env)->NewStringUTF(env,&done);
free(done);
return answer;
}
JNIEXPORT jstring JNICALL Java_SenseHat_getHumidity(JNIEnv *env, jobject thisObj) {
char done[9] = "humidity";
jstring answer;
/* Make a new String based on done, then free done. */
answer = (*env)->NewStringUTF(env,&done);
free(done);
return answer;
}
JNIEXPORT jstring JNICALL Java_SenseHat_getOrientation(JNIEnv *env, jobject thisObj) {
char done[12] = "orientation";
jstring answer;
/* Make a new String based on done, then free done. */
answer = (*env)->NewStringUTF(env,&done);
free(done);
return answer;
}
我使用下面的命令編譯它:
gcc -I /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/include/ -I /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/include/linux/ -shared -o libSenseHat.so SenseHat.c
你能提供編譯器輸出嗎? – Sergio