Search code examples
cmakefilejava-native-interface

MakeFile: error: <jni.h>: No such file or directory


I am trying to call java from c, and I have made the following MakeFile:

include ../../Makefile.defs

auto_gen=
NAME=libproto.so
CC=gcc
CFLAGS= -g -Wall -fPIC
LIBS= -L'$(LD_LIBRARY_PATH)' -ljvm -I"/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.91.x86_64/include/" -I"/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.91.x86_64/include/linux" -I"/usr/local/lib64/kamailio/"
include ../../Makefile.modules


SOURCE=jni_wrapper.c ProtoType.c
OBJECTS=$(SOURCE:.c=.o)

all: $(SOURCE) $(NAME)

%.o: %.c
    $(CC) $(CFLAGS) -c $(LIBS) $<

clean:
    rm -f $(EXEC); rm -f *~; rm -f .*.swp; rm -f .*.swo; rm -f *.o

java:
    javac ProtoType.java
    jar cf ProtoType.jar ProtoType.class
    javap -s -p ProtoType > sigs.txt
    cat sigs.txt

When I compile with make I get an error like this:

error: <jni.h>: No such file or directory

I looked through many stackoverflow pages with a similar problem but they all have same solution which I already had implemented. They said you need to link the library path to jni.h.

As you can see in my MakeFile this is being done:

LIBS= -L'$(LD_LIBRARY_PATH)' -ljvm -I"/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.91.x86_64/include/" -I"/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.91.x86_64/include/linux" -I"/usr/local/lib64/kamailio/"

I triple checked the directories and the permissions and everything is fine.

Any Suggestions?


Solution

  • You need to add the end of your LIBS definition to the CFLAGS

    CFLAGS= -g -Wall -fPIC -I"/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.91.x86_64/include/" -I"/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.91.x86_64/include/linux" -I"/usr/local/lib64/kamailio/"
    LIBS= -L'$(LD_LIBRARY_PATH)' -ljvm  
    

    The -I include directories are used by the compiler not the linker. It's the compiler that can't find your .h file.

    You may also want to change the targets as follows

    %.o: %.c
        $(CC) $(CFLAGS) -c $<
    $(NAME): $(OBJECTS)
        $(CC) $(OBJECTS) -o $@ $(LIBS)
    

    This will build you .so file.