So far we have seen how to code our own C++ functions and link them to JNI in a program. However, sometimes you may find it useful to use functions already implemented into third-party APIs. Third-party APIs are application programming interfaces that are designed by third parties (often large corporations) to add functionality to your own applications. You may find a lot of examples, particularly within mobile development such as Android development, where third-party APIs are used to add services such as location, Google services, etc. to a phone’s operating system. Third-party API integration is a great way to save time by allowing programmers to reuse methods that have already been created instead of developing their own implementation to deliver similar, if not the same, functionality.

Third-party APIs can be linked to Java applications using JNI. To do that it is crucial that all third-party .dll, .jar, and .so files are placed in the native directory on the computer you are running your program on. It is also important that you include the path to the third-party files in the compilation of your program. Take a look at the following example of code to run in your terminal:


where pathname refers to the path leading to the folder housing the dynamic library files.


Take a look at the flowchart to see how Java methods use JNI to call C++ functions, and vice versa! Note, that our example in this exercise implies that the JNI wrapper exists separate from your .cpp files, and embedded within your .java file.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?