Java – Do you need to provide implementations for all native methods?

Do you need to provide implementations for all native methods?… here is a solution to the problem.

Do you need to provide implementations for all native methods?

It could not be found by a quick google search for “Java unimplemented native method” and the title of the issue. Some sources say that you have to provide an implementation, but then they continue to call functions that they already implement, so it is logical that they need a separate implementation.

The background of my question is slightly different.

I

have a Java library that I want to use on Windows, Unix, and Android.
I have JNI for implementing the native library. For Android, this library comes with specialized logging. This feature is only available for Android, not Windows/UNIX. If I don’t call the log function, can I declare a public static native void log() function without providing an implementation?

This will allow me to provide the same libraries for Windows, Unix, and Android. In any case, no one will invoke Android logging on Windows/UNIX.


TL;DR: I have a class that contains methods public static native void a() and public static native void b(). If I intend to call a() on platforms A, B, and C, and only intend to call b() on platform C, do I have to call b() on platforms A and B?

Solution

No implementation is required. You don’t even need to compile header files for methods that aren’t implemented.

However, normally, if you call this function, you will get a java.lang.UnsatisfiedLinkError.

Related Problems and Solutions