Creating JNI Header Files for Class Files in JDK 10

An integral part of the Java Native Interface (JNI) is to link the JVM code and native code through the C headers. The way to generate these header files was pretty simple: just call the command line utility javahin the class files. This process then generates prototypes for any method marked with the modifier native.

As with Java 10, the utility javah

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

+9
source share
4 answers

gjavah JNI.

+5

javap. . , , , , .

#!/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}

, .

, , Java 10 , Java, , - . , .

+5

You can always turn jars and class files into source code using the free Telerik Just Decompile tool. This wonderful product worked on every jar I have ever tried. This is an open source world. Then use the header generator of your choice.

0
source

I think the best solution is to simply install jdk8. And no need to remove jdk10, just change the environment variable.

-3
source

Source: https://habr.com/ru/post/1695391/


All Articles