javac “no source files” when using -h option

给你一囗甜甜゛ 提交于 2019-11-28 11:24:39

In Java 8, you had to make intermediate step of generating class files to get C headers

Lets say you have following structure

recipeNo001
├── Makefile
├── README.md
├── c
│   └── recipeNo001_HelloWorld.c
├── java
│   └── recipeNo001
│       └── HelloWorld.java
├── lib
└── target

In Java (prior to JDK 9) you had to compile class and use javah with compiled sources

> 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

In Java 9 you can use javac -h with Java source code

> 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

The solution I discovered was that I was not specifying the directory where javac should place the header files.

Executing javac -h . NativeTest.java worked.

The javah tool has been superseded by the "javac -h" feature . we should be able to just use the normal Java compiler (with the -h flag in Java 8+) to output those files during the Java compilation step.

Usage: -h directory Specifies where to place generated native header files.

When you specify this option, a native header file is generated for each class that contains native methods or that has one or more constants annotated with the java.lang.annotation.Native annotation. If the class is part of a package, then the compiler puts the native header file in a subdirectory that reflects the package name and creates directories as needed.

javac -h directory name NativeTest.java will solve the problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!