Compiling from a file using jni.h

I am having trouble compiling the following program

PPConverter.java:

 public class PPConverter {
    private native void convert(String s);
    public static void main(String[] args){
        new PPConverter().convert(args[0]);
    }
    static {
        System.loadLibrary("converter");
    }
}

converter.c:

 #include <jni.h>
 #include <stdio.h>
 #include "PPConverter.h"

 JNIEXPORT void JNICALL Java_PPConverter_convert (JNIEnv *, jobject, jstring){
    printf(jstring);
    return;
  }

Since I'm working on UNIX, I use the following command to compile the converter.c file:

cc -I/usr/lib/jvm/java-6-openjdk/include  converter.c -o libconverter.so

but I get the following errors:

converter.c: In function รขJava_PPConverter_convertรข:
converter.c:5: error: parameter name omitted
converter.c:5: error: parameter name omitted
converter.c:5: error: parameter name omitted
converter.c:6: error: expected expression before รขjstringรข

What am I doing wrong?

+5
source share
2 answers

In case someone encounters this error, the problem is that the header file created by javah does not indicate the name of its parameters (it is just a header file, not an implementation). But in your implementation, if you just copy / paste the header file without adding parameter names, you will get an error message.

So, the code from your header file (the file created by javah does not change this file):

JNIEXPORT void JNICALL Java_PPConverter_convert (JNIEnv *, jobject, jstring);

( , .c .cpp), :

JNIEXPORT void JNICALL Java_PPConverter_convert (JNIEnv *env, jobject obj, jstring mystring){

.

+8
JNIEXPORT void JNICALL Java_PPConverter_convert (JNIEnv *jbi, jobject obj, jstring str){
   printf(jstring);
   return;
  }

maby u -

0

All Articles