90

In Ubuntu 12.04, I have jdk7 from sun/oracle installed. When locate jni.h, it prints multiple locations

/usr/lib/jvm/java-6-openjdk-amd64/include/jni.h
/usr/lib/jvm/jdk1.7.0_07/include/jni.h
...

In the header file generated by JDK, there is include <jni.h>, and currently it complains

fatal error: jni.h: No such file or directory.

In my Makefile, there is no specification of locations where jni.h is. And I am asking if possible to configure certain system parameter to make path of jni.h (say, /usr/lib/jvm/jdk1.7.0_07/include/jni.h) to be known when being compiled.

4
  • 4
    Add -I/usr/lib/jvm/jdk1.7.0_07/include to compiler flags.
    – hmjd
    Jan 25, 2013 at 20:22
  • 1
    is there any system-wise configuration for path of jni.h, so that no need to specify compile option everytime it gets compiled.
    – Richard
    Jan 25, 2013 at 20:24
  • 1
    Possible setting JVM_HOME environment variable and referring to it from the makefile (I think): -I$JVM_HOME/include
    – hmjd
    Jan 25, 2013 at 20:26
  • The next stage after this question: stackoverflow.com/questions/6804253/…
    – Raedwald
    Dec 24, 2014 at 15:38

11 Answers 11

52

You have to tell your compiler where is the include directory. Something like this:

gcc -I/usr/lib/jvm/jdk1.7.0_07/include

But it depends on your makefile.

2
  • Locate jni.h returns me nothing. I have java 1.7 installed on Ubuntu 13.04 machine.
    – w2lame
    Nov 14, 2013 at 12:33
  • To generalize jdb's solution: first set an environment $JAVA_HOME to point to your desired jdk folder. Then make sure to include the path $JAVA_HOME/include in your include paths for the gcc call. Nov 19, 2018 at 22:48
33

It needs both jni.h and jni_md.h files, Try this

gcc -I/usr/lib/jvm/jdk1.7.0_07/include \
  -I/usr/lib/jvm/jdk1.7.0_07/include/linux filename.c

This will include both the broad JNI files and the ones necessary for linux

1
  • Hey! I'm on mac and gcc -I/Library/Java/JavaVirtualMachines/jdk1.8.0_73.jdk/Contents/Home/include/ gives me following error: clang: error: no input files Apr 20, 2016 at 7:28
18

Installing the OpenJDK Development Kit (JDK) should fix your problem.

sudo apt-get install openjdk-X-jdk

This should make you able to compile without problems.

2
  • E: Unable to locate package openjdk-X-jdk Feb 26, 2021 at 10:40
  • 5
    You're supposed to replace the X with the java version you need. Mar 24, 2021 at 13:54
12

I usually define my JAVA_HOME variable like so:

export JAVA_HOME=/usr/lib/jvm/java/

Therein are the necessary include files. I sometimes add the below to my .barshrc when I compile a lot of things that need it.

9

Use the following code:

make -I/usr/lib/jvm/jdk*/include

where jdk* is the directory name of your jdk installation (e.g. jdk1.7.0).

And there wouldn't be a system-wide solution since the directory name would be different with different builds of JDK downloaded and installed. If you desire an automated solution, please include all commands in a single script and run the said script in Terminal.

4
  • This answer (though not voted) is the correct solution to this question. Oct 27, 2013 at 12:55
  • Locate jni.h returns me nothing. I have java 1.7 installed on Ubuntu 13.04 machine.
    – w2lame
    Nov 14, 2013 at 12:37
  • 1
    I suspect this is going to fail hard if there is more than one jdk installation
    – dequis
    Oct 21, 2014 at 2:38
  • On Fedora, to locale jni.h, type sudo dnf repoquery --installed -l java-latest-openjdk-devel Jun 23, 2021 at 16:55
7

For me it was a simple matter of being sure to include the JDK installation (I'd only had the JRE). My R CMD javareconf output was looking like:

Java interpreter : /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
Java version     : 1.8.0_191
Java home path   : /usr/lib/jvm/java-8-openjdk-amd64/jre
Java compiler    : not present
Java headers gen.:
Java archive tool:

trying to compile and link a JNI program
detected JNI cpp flags    :
detected JNI linker flags : -L$(JAVA_HOME)/lib/amd64/server -ljvm
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG      -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c conftest.c -o conftest.o
conftest.c:1:17: fatal error: jni.h: No such file or directory
compilation terminated.
/usr/lib/R/etc/Makeconf:159: recipe for target 'conftest.o' failed
make: *** [conftest.o] Error 1
Unable to compile a JNI program


JAVA_HOME        : /usr/lib/jvm/java-8-openjdk-amd64/jre
Java library path:
JNI cpp flags    :
JNI linker flags :
Updating Java configuration in /usr/lib/R
Done.

And indeed there was no include file in my $JAVA_HOME. Very simple remedy:

sudo apt-get install openjdk-8-jre openjdk-8-jdk

(note that this is specifically intended to install the openJDK and not the one from Oracle)

Afterwards all is well:

Java interpreter : /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
Java version     : 1.8.0_191
Java home path   : /usr/lib/jvm/java-8-openjdk-amd64/jre
Java compiler    : /usr/lib/jvm/java-8-openjdk-amd64/jre/../bin/javac
Java headers gen.: /usr/lib/jvm/java-8-openjdk-amd64/jre/../bin/javah
Java archive tool: /usr/lib/jvm/java-8-openjdk-amd64/jre/../bin/jar

trying to compile and link a JNI program
detected JNI cpp flags    : -I$(JAVA_HOME)/../include -I$(JAVA_HOME)/../include/linux
detected JNI linker flags : -L$(JAVA_HOME)/lib/amd64/server -ljvm
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -I/usr/lib/jvm/java-8-openjdk-amd64/jre/../include -I/usr/lib/jvm/java-8-openjdk-amd64/jre/../include/linux     -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c conftest.c -o conftest.o
g++ -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o conftest.so conftest.o -L/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server -ljvm -L/usr/lib/R/lib -lR


JAVA_HOME        : /usr/lib/jvm/java-8-openjdk-amd64/jre
Java library path: $(JAVA_HOME)/lib/amd64/server
JNI cpp flags    : -I$(JAVA_HOME)/../include -I$(JAVA_HOME)/../include/linux
JNI linker flags : -L$(JAVA_HOME)/lib/amd64/server -ljvm
Updating Java configuration in /usr/lib/R
Done.
1
  • after I did the steps you recommended I also added following lines to my Cmakelists.txt file: include_directories(/usr/lib/jvm/java-8-openjdk-amd64/include /usr/lib/jvm/java-8-openjdk-amd64/include/linux) link_directories(/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server) then deleted the Cmake cache and built the project again, then everything went well Jun 15 at 9:58
5

Setting JAVA_INCLUDE_DIR to where jni.h is located should solve your problem (setting CPPFLAGS did not work for me)

Assuming it is /usr/lib64/java/include;

export JAVA_INCLUDE_DIR=/usr/lib64/java/include
1
  • I use build.xml for some older project and this didn't work for me, I had to provide <arg value="-I/opt/oracle-jdk-bin-1.8.0.152/include"/> and <arg value="-I/opt/oracle-jdk-bin-1.8.0.152/include/linux"/> to make it working Jan 22, 2018 at 13:16
3

Above answers give you a hardcoded path solution. This is bad on so many levels (java version change, OS change, etc).

Cleaner solution is to add:

JAVA_HOME = $(shell dirname $$(readlink -f $$(which java))|sed 's^jre/bin^^')

near the top of your makefile, then add:

-I$(JAVA_HOME)/include

To your include flags.

I am posting this because I ran into the same problem and spent too much time googling for wrong answers (I am building an app on multiple platforms so the build environment needs to be transportable).

7
  • Do you mean I should add -I$(JAVA_HOME)/include to make.sh? Doing that gave me an error.
    – Pramod
    Jun 24, 2019 at 10:57
  • What error? If you follow the steps I outlined, there should be no error, unless JAVA_HOME is not defined (which might happen for a non-supported install of java). Also, is your default make shell sh or bash? You need to be more specific.
    – GrueMaster
    Jun 28, 2019 at 16:22
  • I was using cmd :| .
    – Pramod
    Jun 29, 2019 at 19:17
  • cmd??? That's Windows. Maybe I should be more clear and say that this will work with different Linux OSs. Also, the original question was Linux based, no mention of Windows.
    – GrueMaster
    Jul 19, 2019 at 17:19
  • @Promod This can't be for real. Jul 22, 2019 at 17:02
1

None of the posted solutions worked for me.

I had to vi into my Makefile and edit the path so that the path to the include folder and the OS subsystem (in my case, -I/usr/lib/jvm/java-8-openjdk-amd64/include/linux) was correct. This allowed me to run make and make install without issues.

0

In case you are on Ubuntu:

#X means 6,7,8...
apt install openjdk-X-jdk
-2

I don't know if this applies in this case, but sometimes the file got deleted for unknown reasons, copying it again into the respective folder should resolve the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.