Help in Ubuntu JNI

1

Can you help me compile the GCC for JNI in Ubuntu ? What happens is that when creating the . "So" , and compile by calling the necessary libraries, in my case (for the Java version I use) ...

gcc -shared -I/usr/lib/jvm/java-8-oracle/include/ -I/usr/lib/jvm/java-8-oracle/include/linux/ -o libHolaMundo.so HolaMundo.c -Wall

... I get the error that:

  

In file included from HelloWorld.c: 3: 0:

     

HolaMundo.h: 2: 17: fatal error: jni.h: No such file or directory

     

#include <jni.h>

    
asked by Edú Arias 10.11.2016 в 00:32
source

1 answer

0

Using JNI for the first time can be a bit complicated, I recommend using JNA. From JNA you can call native code without recompiling the executable or modifying it.

I give you an example with JNA:

Code in C:

#include  //se usa la entrada por defecto stdio.h

void helloFromC() {
    printf("Hello from C!\n");
}

To compile: gcc -o libctest.so -shared ctest.c

To use this library from Java:

import com.sun.jna.Library;
import com.sun.jna.Native;

public class HelloWorld {
    public interface CTest extends Library {
        public void helloFromC();
    }

    static public void main(String argv[]) {
        CTest ctest = (CTest) Native.loadLibrary("ctest", CTest.class);
        ctest.helloFromC();
    }
}

More information:

JNA project page

JNA: Wikipedia

Regarding your problem: The compiler could not find the header file jni.h, put it in the header folder and recompile it.

    
answered by 10.11.2016 / 03:19
source