Search code examples
javajava-native-interface

Howto create h-files with stable names using javac


I've got a Java class which starts with

package examples.logging;

public class FastLogging {

    // Logging class

    public static native long logging_new(int level, String domain, boolean console, String file, String server,
            String connect, int max_size, int backlog);

    public static native void logging_set_level(long instance_ptr, int level);
...

When I run javac -h . examples/logging/FastLogging.java I get the following output:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class examples_logging_FastLogging */

#ifndef _Included_examples_logging_FastLogging
#define _Included_examples_logging_FastLogging
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     examples_logging_FastLogging
 * Method:    logging_new
 * Signature: (ILjava/lang/String;ZLjava/lang/String;Ljava/lang/String;Ljava/lang/String;II)J
 */
JNIEXPORT jlong JNICALL Java_examples_logging_FastLogging_logging_1new
  (JNIEnv *, jclass, jint, jstring, jboolean, jstring, jstring, jstring, jint, jint);

/*
 * Class:     examples_logging_FastLogging
 * Method:    logging_set_level
 * Signature: (JI)V
 */
JNIEXPORT void JNICALL Java_examples_logging_FastLogging_logging_1set_1level
  (JNIEnv *, jclass, jlong, jint);
...

How can I avoid that javac is inserting the "1" in the created function names, like Java_examples_logging_FastLogging_logging_1set_1level?


Solution

  • The JNI naming convention is very stable and hasn't changed in many years.

    For some reason the 1 in the generated method name seem to be an issue. While I don't understand what is the problem if you want avoid them just avoid using underscores in your native method names. See the JNI method naming specification to understand that and other transformations that are expected:

    Underscore (_, U+005F) -> _1

    By the way Java conventions don't use underscores in method names so it would be a good practice to avoid using them altogether.