简体   繁体   中英

Is providing an implementation for all native methods required?

Couldn't find this via a quick google search on "java unimplemented native methods" and the question title. Several sources say you must provide an implementation, but then they go on to call the function they've implemented, so it's kinda logical that they need a separate implementation.

My question has a slightly different context.

I have a Java Library that I want to use on Windows, Unix and Android. I have JNI for implementing a native library. For android, this library comes with specialized logging. This function is only present for android, and not for windows/unix. Can I declare a public static native void log() function and NOT provide an implementation, provided that I don't call the log function?

This would allow me to provide the same library for windows, unix and android. Nobody would be calling the android log function on windows/unix anyway.


TL;DR: I have a class containing a method public static native void a() and public static native void b() . If I intend to call a() on platforms A, B and C, and intend to call b() only on platform C, do I have to provide an implementation for b() on platforms A and B?

It is not required to provide an implementation. You don't even need to compile a header file for your unimplemented methods.

However, as is normal, you will get a java.lang.UnsatisfiedLinkError if you call the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM