11

An integral part of the Java Native Interface (JNI), is the bridging of JVM code and native code through C headers. The way to generate these header files used to be quite straight forward: simply call the command line utility javah on class files. This process would then generate prototypes for any method marked with the native modifier.

As of Java 10 however, the javah utility has been removed, and its suggested replacement is a new flag "-h" to javac. The replacement works fine if one has the Java source files available, however falls short in cases where only compiled class files are available. (The issue that sparked this question is that I'm trying to generate JNI bindings from Scala sources. My current approach has been to compile them first and then run javah over the resulting class files.)

In a situation where only compiled class files are available, is there a way to generate C header files, similar to the way javah used to?

Jakob Odersky
  • 1,371
  • 11
  • 23
  • 1
    Might just be hypothetical, but is compiling the scala sources into a *jar* and then using the jar on *classpath* for `javac -h` flag not an option? – Naman Mar 27 '18 at 07:32
  • 1
    There is an ugly solution based on `javap`. You can decompile class, leave only native things and use javac to compile things back. But it's quite ugly approach. – Oo.oO Mar 27 '18 at 08:51
  • @nullpointer, unfortunately your proposed solution doesn't work. javac really requires native modifiers in source code. Currently mko's answer is looking the most promising. – Jakob Odersky Mar 27 '18 at 18:34
  • 1
    Similar question, regarding use of javah for Kotlin: https://stackoverflow.com/questions/48816188/kotlin-replacement-for-javah – Jakob Odersky Apr 11 '18 at 03:03

3 Answers3

7

You can always go via javap. I know, I know. It's ugly, has lots of assumptions, but in case you desperately need to generate headers for lots of files it might be the only option.

#!/bin/bash

# FIRST_ARG - full class name (with package)
# SECOND_ARG - class path

CLASS_NAME=`javap -cp $2 $1 | \
  grep -v "Compiled from" | \
  grep "public class" | \
  cut -f3 -d" " | \
  awk -F"." '{ print $NF }'`

PACKAGE_NAME=`javap -cp $2 $1 | \
  grep -v "Compiled from" | \
  grep "public class" | \
  cut -f3 -d" " | \
  sed s/\.${CLASS_NAME}$//`

DIR_NAME=`echo $PACKAGE_NAME | sed 's|\.|/|g'`
mkdir -p java_jni/${DIR_NAME}

JAVA_FILE_NAME="java_jni/${DIR_NAME}/${CLASS_NAME}.java"

echo "package ${PACKAGE_NAME};" > ${JAVA_FILE_NAME}
echo "public class ${CLASS_NAME} {" >> ${JAVA_FILE_NAME}

javap -cp $2 $1 | grep "native" | while read line; do
  param=0
  comma=`echo $line | grep "," | wc -l`
  while [ $comma -gt 0 ]; do
    line=`echo $line | sed "s/,/ param_${param}|/"`
    let param=param+1
    comma=`echo $line | grep "," | wc -l`
  done
  line=`echo $line | sed "s/)/ param_${param})/" | sed 's/|/,/g'`
  echo "  $line" >> ${JAVA_FILE_NAME}
done

echo "}" >> ${JAVA_FILE_NAME}

mkdir -p c_header
javac -h c_header ${JAVA_FILE_NAME}

I bet it can be made way more beautiful.

For me, now, when I slowly start to think about inevitable move towards Java 10, and all these cases, where I might be surprised by non existing Java source code, I think it's not a bad idea to have some tool at my disposal. Just in case.

Oo.oO
  • 12,464
  • 3
  • 23
  • 45
6

We can use gjavah to generate JNI header files.

jamessan
  • 41,569
  • 8
  • 85
  • 85
Glavo
  • 448
  • 3
  • 13
-5

The best solution is just install a jdk8, i think. And no need to uninstall jdk10, just modify the environment variable.

W. X
  • 129
  • 2
  • 7