Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compiling c file that uses 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 am working on UNIX, I am using 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 am getting 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??

like image 737
twidizle Avatar asked Jan 06 '11 03:01

twidizle


People also ask

What is JNI h file?

JNI Components h is a C/C++ header file included with the JDK that maps Java types to their native counterparts. javah automatically includes this file in the application header files. JNI data type mapping in variables. Code: boolean jboolean.

What is Jniexport and Jnicall?

In short, JNIEXPORT contains any compiler directives required to ensure that the given function is exported properly. On android (and other linux-based systems), that'll be empty. JNICALL contains any compiler directives required to ensure that the given function is treated with the proper calling convention.

What is JNA vs JNI?

Java Native Access (JNA) is a community-developed library that provides Java programs easy access to native shared libraries without using the Java Native Interface (JNI). JNA's design aims to provide native access in a natural way with a minimum of effort. Unlike JNI, no boilerplate or generated glue code is required.


1 Answers

In case anyone runs into this error, the problem is that the header file created by javah doesn't specify the name of its parameters (it's just a header file not an implementation). But in your implementation, if you just copy/paste the header file without adding the parameter names you'll get the error.

So the code from your header file (the file generated by javah, don't change this file):

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

When you copy it (into your file ending in .c or .cpp), needs to have variable names:

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

This will fix it.

like image 140
testerolift Avatar answered Sep 22 '22 07:09

testerolift