Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin replacement for javah

javah has been deprecated since JDK 8 and will be/has been removed in JDK 10, and according to JEP 313 and the deprecation text, javac with the -h flag should be used instead:

Warning: The javah tool is planned to be removed in the next major JDK release. The tool has been superseded by the '-h' option added to javac in JDK 8. Users are recommended to migrate to using the javac '-h' option; see the javac man page for more information.

The problem is, javah operates on compiled .class files, while javac operates on source files (i.e. .java files.)

javah works fine with Kotlin and external functions, since everything ends up compiled as Java bytecode, but since there aren't any Java source files when using Kotlin, I don't see any way javac -h could work.

Is there a javah replacement, or a workaround, for Kotlin?

like image 818
Salem Avatar asked Feb 15 '18 21:02

Salem


Video Answer


2 Answers

I recommend gjavap.
In the future I will also implement an easier-to-use command line tool which provides similar functionality to javap.

like image 99
Glavo Avatar answered Sep 27 '22 10:09

Glavo


There is currently no built-in way to do this. There is an open issue for this on the Kotlin issue tracker that was raised in November 2019, but as of now it has not been prioritized and doesn't have a target version.

The only way to produce the header with JDK 10+ is to use javac -h, which only works for Java source code, not Kotlin. I tested the method in How to solve missing javah in Java 10 – ugly way linked by Oo.oO, and it works as a workaround for now. The steps are:

  1. Use javap to decompile the bytecode back into Java
  2. Edit the decompiled Java to make it suitable for producing the header file (reducing the file to just the native method definitions).
  3. Run the decompiled Java code through javac -h to produce the header

I'm thinking of writing a gradle script to do this (or hoping somebody else beats me to it!). If I manage to get it done, I'll update this post.

like image 3
Adam Millerchip Avatar answered Sep 26 '22 10:09

Adam Millerchip