Learn

In the previous exercise, we learned about how programs can be coded to integrate C++ and Java. Now, let’s take a look at how to run such a cross-platform application! It is first necessary to have an understanding of how the following components work to run such an application:

  1. Java compiler: An example of a Java compiler includes calling the command javac _____ from the terminal.
  2. Java Virtual Machine (JVM): A JVM is commonly pre-installed on a computer. It is the means by which a Java application can run on different operating systems.
  3. Native header files: These are generated when compiling the Java program using the -h command.
  4. Native method C generator: The native method in C++ is generated in the header file.
  5. All library files that have been called in your program.

Running a program that uses JNI looks different based on which operating system the program is intended to run on. To begin, start by running the following command in your terminal to confirm that the JAVA_HOME variable has been set: echo $JAVA_HOME Let’s look at the steps you will follow to compile and run a program in the table below:

Operating System Steps
Windows 1. Compile the C++ file and create shared libraries using the following command: gcc -Wl,--kill-at -shared -IC:/JDK/include -IC:/JDK/include/win32 -IC:/dll -o C:/JNI_project/mynativelib.dll C:/JNI_project/jnitest.cpp
2. Run your program with the following command in your terminal: java -Djava.library.path=C:JNI_project FindSum
Linux 1. Compile the C++ file and create shared libraries using the following command: gcc -I/usr/lib/jvm/java-8-oracle/include -I/usr/lib/jvm/java-8-oracle/include/linux -I/home/user1/JNI_project/ -o /home/user1/JNI_project/mynativelib.so /home/user1/JNI_project/FindSum.cpp
2. Run your program with the following command in your terminal: java -Djava.library.path=/home/user1/JNI_project FindSum

Instructions

Take a moment to look at the program we coded in the last exercise on your own! If you get stuck, refer back to the steps outlined in this exercise. The files have been provided in the code editor for you.

Take this course for free

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?