Translating Java code that uses JNI

120 views
Skip to first unread message

kru...@lesspain.de

unread,
Aug 21, 2016, 7:10:36 AM8/21/16
to j2objc-discuss
Hi,

could someone tell me if it is possible to use j2objc with existing Java code that uses JNI to access native C or C++ libraries?

Thanks in advance,

Robert

Tom Ball

unread,
Aug 21, 2016, 2:56:40 PM8/21/16
to j2objc-discuss
Yes, there are a couple of options. If you translate the Java source unaltered, then native methods have the same JNI signatures as javac would generate. Here's the java.util.regex.Pattern Java source, and its associated JNI code. The JNI file is just C code, and only has a ".m" suffix to make the build simpler.

Much of the native methods in jre_emul uses OCNI, modeled after GWT's JSNI, which embeds Objective C into specially formatted comments between the native method declaration and its closing semi-colon. This works cross-platform, since Java compilers like javac will just treat OCNI sections as non-javadoc comments. However, no IDE supports OCNI, so you lose the benefits of syntax checking and formatting. Here's an example.

Be aware that j2objc doesn't/cannot support the full JNI API, since it requires a Java virtual machine or garbage collector (iOS licensing forbids the former and strongly discourages the latter). Our JNI support was created to meet the minimum needs of libraries as apps used them, though, so there may be missing JNI function calls that can be implemented natively for iOS. Try compiling your JNI native code with j2objc/jre_emul/Classes/jni.h (in the distribution, it's include/jni.h), and if any functions are missing that don't need a JVM or GC, consider adding them to jni.m and sending a pull request to contribute them.

--
You received this message because you are subscribed to the Google Groups "j2objc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to j2objc-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Krüger

unread,
Aug 22, 2016, 2:36:01 AM8/22/16
to j2objc-...@googlegroups.com
Thanks a lot, Tom for the in-depth answer!

I need to share this with the guy who develops our JNI framework which we use to drive all our media libraries where native code is used extensively.

To unsubscribe from this group and stop receiving emails from it, send an email to j2objc-discuss+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "j2objc-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/j2objc-discuss/lIBjhXNFK1M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to j2objc-discuss+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Robert Krüger
Managing Partner
Lesspain GmbH & Co. KG

Reply all
Reply to author
Forward
0 new messages