/jni-headers-maven-plugin

Maven plugin for generating JNI headers

Primary LanguageJavaOtherNOASSERTION

Maven plugin for generating JNI headers

Maven plugin for generating C/C++ header files for native functions called from Java (using javah) and for java methods called from native code (using javap). It's designed to run during compile maven phase after java sources compilation, but before native sources compilation (maven plugin order within the same phase is defined by plugins position in pom.xml file).

####Headers for native methods

Headers for native methods are generated calling javah with maven-provided arguments.

####Headers for java methods

Typical access for java methods from native code looks like:

jclass java_class = (*env)->GetObjectClass(env, obj);
jmethodID my_java_method = (*env)->GetMethodID(env, my_class, "my_method", "(Ljava/lang/String;)V");

This plugin uses javap to generate header with macros definitions for method names (my_method) and method signatures ((Ljava/lang/String;)V). So with generated header native code will look like:

jclass java_class = (*env)->GetObjectClass(env, obj);
jmethodID my_java_method = (*env)->GetMethodID(env, my_class, MY_METHOD_NAME, MY_METHOD_NAME_SIGNATURE);

Plugin is available in Maven cental.

Maven-generated site is available here.

Usage example

<!-- ensure java compilation beforehand -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
</plugin>
<plugin>
    <groupId>com.alexkasko.maven</groupId>
    <artifactId>jni-headers-maven-plugin</artifactId>
    <version>1.0.6</version>
    <executions>
        <!-- generate header for native methods -->
        <execution>
            <id>javah</id>
            <phase>compile</phase>
            <goals>
                <goal>javah</goal>
            </goals>
            <configuration>
                <javahClass>org.foo.MyClass</javahClass>
                <javahOutputFilePath>${project.basedir}/jni/mymodule/include/my_header_jni.h</javahOutputFilePath>
            </configuration>
        </execution>
        <!-- generate header for java methods -->
        <execution>
            <id>javap</id>
            <phase>compile</phase>
            <goals>
                <goal>javap</goal>
            </goals>
            <configuration>
                <javapClass>org.foo.MyClass</javapClass>
                <javapOutputFilePath>${project.basedir}/jni/mymodule/include/my_header_callbacks.h</javapOutputFilePath>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <!-- native code compilation here -->
    ...
</plugin>

my_header_jni.h will contain usual javah output. my_header_callbacks.h will look like this:

/* DO NOT EDIT THIS FILE - it is machine generated */
/* Header for class org_foo_MyClass */

#ifndef _Callbacks_org_foo_MyClass
#define _Callbacks_org_foo_MyClass

/* public abstract void callbackExit(java.lang.String); */
#define CALLBACK_EXIT_NAME "callbackExit"
/*   Signature: (Ljava/lang/String;)V */
#define CALLBACK_EXIT_SIGNATURE "(Ljava/lang/String;)V"

/* public abstract int callbackAudioInit(int, int, int, int); */
#define CALLBACK_AUDIO_INIT_NAME "callbackAudioInit"
/*   Signature: (IIII)I */
#define CALLBACK_AUDIO_INIT_SIGNATURE "(IIII)I"

#endif //_Callbacks_org_foo_MyClass

License information

This project is released under the Apache License 2.0

Changelog

1.0.6 (2013-06-12)

  • another fix for windows (#1)

1.0.5 (2013-06-08)

  • fix javap and javah search for windows (#1)

1.0.4 (2013-06-08)

  • logging for JDK_HOME search

1.0.3 (2013-06-05)

  • JDK_HOME validation fix #1

1.0.2 (2013-04-14)

  • check source files 'lastModified' and skip execution

1.0.1 (2013-04-13)

  • javap definitions quotation

1.0 (2013-04-11)

  • initial public version