4

I already tried passing this argument in the pom but it does not recognize it. Since jdk 8 we dont have to manually run javah to generate header files rather we can pass -h argument to javac and generate the header files when compiling. So if I can pass "-h dir" to maven compiler plugin I can generate the header files when I run mvn compile

<plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.7.0</version>
      <configuration>
        <compilerArgs>
          <arg>-verbose</arg>
          <arg>-h .</arg>
        </compilerArgs>
      </configuration>
</plugin>

But when I run mvn install I get this

[INFO] BUILD FAILURE

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 2.936 s

[INFO] Finished at: 2018-11-07T14:52:49+05:30

[INFO] Final Memory: 9M/155M

[INFO]------------------------------------------------------------------------

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project adiesha-native: Fatal error compiling: invalid flag: -h . -> [Help 1]

Is there any other way to do this with maven or do i have to manually create them using "javac -h dir"

Any help would be greatly appreciated

Adiesha
  • 133
  • 2
  • 9
  • 5
    try `-h .` (two args). And please dont post links to screenshots on SO, rather paste the real code in the question (most people will not bother to click on external links) – Gyro Gearless Nov 07 '18 at 09:13
  • @GyroGearless Thanks It worked. I will edit the post. (Thanks for the advice) – Adiesha Nov 07 '18 at 09:20
  • I would rather run `javac -h` manually. Having the automagically synchronized headers is, most likely, not enough. If something changes in the interface, you will need to adjust the C++ implementations, too. It may be nice to verify that the autogenerated headers are not changed, but this involves a separate custom build step to run diff against the current git copy of same header files. – Alex Cohn Nov 07 '18 at 12:37
  • @AlexCohn That's true. But the thing is it is a cumbersome task to manually run the javac with the relevant comipler options every time. That is why i needed to add this step into the maven compiler plugin life cycle. But what you say is true it is always better to go through the diff before generating the headers. – Adiesha Nov 08 '18 at 05:12
  • I am sorry you are stuck with the tools that are not up to date. I would expect better Java IDEs that will take full advantage of JDK 8 and later. Also, if your native interface is not stable, consider shifting from pure JNI to more flexible frameworks. Examples are [djinni](https://github.com/dropbox/djinni), [SWIG](http://www.swig.org/), [JNA](http://jna.java.net/), and more. – Alex Cohn Nov 08 '18 at 07:28
  • If your project requires tricky compiler options to extract the correct JNI headers, you might need custom scripts to keep track of them. – Alex Cohn Nov 08 '18 at 07:30

1 Answers1

15

I was have the same error "invalid flag: -h target/headers" with my maven build.

I tried the suggestion from Gyro Gearless above. I separated my original '-h target/headers' into '-h and 'target/headers'. That resolved my issue. Now all jni headers within my project are generated correctly and placed within target/headers.

Here is the entire compiler plugin config section from my pom.xml:

        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <compilerArgs>
                    <arg>-h</arg>
                    <arg>target/headers</arg>
                 </compilerArgs>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>

With this pom, the command 'mvn compile' was enough to generate JNI headers. Note that 'mvn clean' will not remove previously generated headers.

NaderNader
  • 1,038
  • 1
  • 11
  • 16