使用-h选项时,javac"没有源文件"

Dav*_*gal 7 c java java-native-interface header-files

我正在尝试使用JNI和JDK 9.我有一个类NativeTest.java看起来像这样的类:

public class NativeTest {

    static {
        System.loadLibrary("hello");
    }

    private native void sayHello();

    public static void main(String[] args) {
        new NativeTest().sayHello();
    }
}
Run Code Online (Sandbox Code Playgroud)

我编译该类,然后用于javah NativeTest生成头文件.

签发后javah,我收到此警告:

Warning: The javah tool is planned to be removed in the next major
JDK release. The tool has been superseded by the '-h' option added
to javac in JDK 8. Users are recommended to migrate to using the
javac '-h' option; see the javac man page for more information.
Run Code Online (Sandbox Code Playgroud)

我知道在下一个主要的JDK版本发布之前还需要一段时间,但我想我现在开始习惯这个新选项.

所以,在尝试javac -h NativeTest.java(以及其它类似的变化NativeTest,NativeTest.class等等),我不断收到此错误:

javac: no source files

我无法在网上找到任何帮助,可能是因为这个功能相对较新,我-h在手册页中找不到任何关于这个新选项的信息.

其他人试试这个吗?我错过了什么?

Dav*_*gal 7

我发现的解决方案是我没有指定javac应放置头文件的目录。

执行javac -h . NativeTest.java工作。


Oo.*_*.oO 6

在Java 8中,您必须进行生成类文件的中间步骤以获取C头

让我们说你有以下结构

recipeNo001
??? Makefile
??? README.md
??? c
?   ??? recipeNo001_HelloWorld.c
??? java
?   ??? recipeNo001
?       ??? HelloWorld.java
??? lib
??? target
Run Code Online (Sandbox Code Playgroud)

在Java(JDK 9之前)中,您必须编译类并使用带编译源的javah

> export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0_11)
> ${JAVA_HOME}/bin/javac -d target java/recipeNo001/*.java
> ${JAVA_HOME}/bin/javah -d c -cp target recipeNo001.HelloWorld
# -d c       -> put generated codes inside c directory
# -cp target -> compiled classes are inside target dir
Run Code Online (Sandbox Code Playgroud)

在Java 9中,您可以使用javac -hJava源代码

> export JAVA_HOME=$(/usr/libexec/java_home -v 9)
> ${JAVA_HOME}/bin/javac -h c java/recipeNo001/HelloWorld.java
# -h c       -> create header file inside c directory
Run Code Online (Sandbox Code Playgroud)