+ some imp.

This commit is contained in:
kelson42 2013-04-02 15:26:07 +02:00
parent 239daec094
commit 7c405a54d3
4 changed files with 53 additions and 9 deletions

View File

@ -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

View File

@ -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;
}
}

13
compile.sh Executable file
View File

@ -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

29
kiwix.c
View File

@ -1,11 +1,32 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jni.h>
#include "JNIKiwix.h"
JNIEXPORT jobject JNICALL Java_JNIKiwix_nativeLoadZIM(JNIEnv *env, jobject obj) {
#include <iostream>
#include <string>
/* 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) {
}