diff --git a/JNIKiwix.h b/JNIKiwix.h index f054c7b4c..640b76621 100644 --- a/JNIKiwix.h +++ b/JNIKiwix.h @@ -10,10 +10,10 @@ extern "C" { /* * Class: JNIKiwix * Method: nativeLoadZIM - * Signature: ()Ljava/lang/Boolean; + * Signature: (Ljava/lang/String;)Z */ -JNIEXPORT jobject JNICALL Java_JNIKiwix_nativeLoadZIM - (JNIEnv *, jobject); +JNIEXPORT jboolean JNICALL Java_JNIKiwix_nativeLoadZIM + (JNIEnv *, jobject, jstring); /* * Class: JNIKiwix diff --git a/JNIKiwix.java b/JNIKiwix.java index d1a21a1d8..03764aa8f 100644 --- a/JNIKiwix.java +++ b/JNIKiwix.java @@ -1,12 +1,22 @@ public class JNIKiwix { - public native Boolean nativeLoadZIM(); + public native boolean nativeLoadZIM(String path); public native String nativeGetContent(String url); static { System.loadLibrary("kiwix"); - } + } public static void main(String[] args) { + JNIKiwix self = new JNIKiwix(); + + try { + self.nativeLoadZIM("test.zim"); + } + catch (Exception e) { + System.out.println("Message " + e.getMessage()); + e.printStackTrace(); + } + return; } } \ No newline at end of file diff --git a/compile.sh b/compile.sh new file mode 100755 index 000000000..247f923b6 --- /dev/null +++ b/compile.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# Compile the JNI class, this will create JNIKiwix.class +javac JNIKiwix.java + +# Create the JNI header file, this will create JNIKiwix.h +javah -jni JNIKiwix + +# Compile the native lib libkiwix.so +g++ -shared -fpic -o libkiwix.so -I/usr/lib/jvm/java-7-openjdk-i386/include/ kiwix.c + +# Run the code +java -Djava.library.path=./ JNIKiwix \ No newline at end of file diff --git a/kiwix.c b/kiwix.c index 04b288b09..e7c6ba289 100644 --- a/kiwix.c +++ b/kiwix.c @@ -1,11 +1,32 @@ -#include -#include -#include #include #include "JNIKiwix.h" -JNIEXPORT jobject JNICALL Java_JNIKiwix_nativeLoadZIM(JNIEnv *env, jobject obj) { +#include +#include + +/* c2jni type conversion functions */ +jboolean c2jni(const bool &val) { + return val ? JNI_TRUE : JNI_FALSE; +} + +jstring c2jni(const std::string &val, JNIEnv *env) { + return env->NewStringUTF(val.c_str()); +} + +/* jni2c type conversion functions */ +bool jni2c(const jboolean &val) { + return val == JNI_TRUE; +} + +std::string jni2c(const jstring &val, JNIEnv *env) { + return std::string(env->GetStringUTFChars(val, 0)); +} + +/* Kiwix library functions */ +JNIEXPORT jboolean JNICALL Java_JNIKiwix_nativeLoadZIM(JNIEnv *env, jobject obj, jstring path) { + return c2jni(true); } JNIEXPORT jstring JNICALL Java_JNIKiwix_nativeGetContent(JNIEnv *env, jobject obj, jstring url) { } +