简体   繁体   English

如何让 jni.h 被找到?

[英]how to make jni.h be found?

In Ubuntu 12.04, I have jdk7 from sun/oracle installed.在 Ubuntu 12.04 中,我安装了来自 sun/oracle 的 jdk7。 When locate jni.h , it prints multiple locationslocate jni.h ,它会打印多个位置

/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 JDK 生成的头文件中有include <jni.h> ,目前报错

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

In my Makefile , there is no specification of locations where jni.h is.在我的Makefile ,没有jni.h所在的位置。 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.我问是否可以配置某些系统参数以使jni.h路径(例如/usr/lib/jvm/jdk1.7.0_07/include/jni.h )在编译时已知。

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.但这取决于您的makefile。

It needs both jni.h and jni_md.h files, Try this它需要jni.hjni_md.h文件,试试这个

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这将包括广泛的 JNI 文件和 linux 所需的文件

Installing the OpenJDK Development Kit (JDK) should fix your problem.安装 OpenJDK 开发工具包 (JDK) 应该可以解决您的问题。

sudo apt-get install openjdk-X-jdk

This should make you able to compile without problems.这应该使您能够毫无问题地进行编译。

Use the following code:使用以下代码:

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

where jdk* is the directory name of your jdk installation (eg jdk1.7.0).其中 jdk* 是 jdk 安装的目录名称(例如 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.并且不会有系统范围的解决方案,因为目录名称会因下载和安装的 JDK 版本不同而不同。 If you desire an automated solution, please include all commands in a single script and run the said script in Terminal.如果您需要自动化解决方案,请将所有命令包含在一个脚本中,并在终端中运行所述脚本。

I usually define my JAVA_HOME variable like so:我通常像这样定义我的 JAVA_HOME 变量:

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.当我编译很多需要它的东西时,我有时会将下面的内容添加到我的 .barshrc 中。

Setting JAVA_INCLUDE_DIR to where jni.h is located should solve your problem (setting CPPFLAGS did not work for me)将 JAVA_INCLUDE_DIR 设置为 jni.h 所在的位置应该可以解决您的问题(设置 CPPFLAGS 对我不起作用)

Assuming it is /usr/lib64/java/include;假设它是/usr/lib64/java/include;

export JAVA_INCLUDE_DIR=/usr/lib64/java/include

For me it was a simple matter of being sure to include the JDK installation (I'd only had the JRE).对我来说,确保包含 JDK 安装很简单(我只有 JRE)。 My R CMD javareconf output was looking like:我的R CMD javareconf输出看起来像:

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 .事实上,我的$JAVA_HOME没有include文件。 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) (请注意,这是专门用于安装openJDK不是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.

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.我不得不vi到我的Makefile和编辑的路径,这样的路径, include文件夹和OS子系统(在我的情况, -I/usr/lib/jvm/java-8-openjdk-amd64/include/linux )是正确的。 This allowed me to run make and make install without issues.这使我可以毫无问题地运行makemake install

Above answers give you a hardcoded path solution.以上答案为您提供了一个硬编码的路径解决方案。 This is bad on so many levels (java version change, OS change, etc).这在很多层面上都很糟糕(Java 版本更改、操作系统更改等)。

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:在 makefile 顶部附近,然后添加:

-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).我发布这个是因为我遇到了同样的问题并且花了太多时间在谷歌上搜索错误的答案(我正在多个平台上构建一个应用程序,所以构建环境需要可移植)。

In case you are on Ubuntu:如果您使用的是 Ubuntu:

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

我不知道这是否适用于这种情况,但有时文件因未知原因被删除,将其再次复制到相应的文件夹中应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM