Working with JNI in Mixed Development Projects

If you have a Java project that uses JNI methods, you can generate a C++ JNI project from the Java project.  The JNI project implements all the native interfaces in Java code.

 To create a C++ JNI Library project:

  1. Open the Java project that uses JNI methods.
  2. In the Project view, right-click a Java class with JNI methods in it and select Generate JNI Library Project.

    The New JNI Library Library Project dialog opens.

  3. Type a project name and optionally change the Project Location and Tool Collection. The default Project Name is CppJNILibrary for a C++ project.
  4. Click Finish.

 

The C++ JNI Library project is created with default implementations of all native functions.

The property java.library.path is added to the Java project's Run properties for the Java VM so that the new C++ library can be loaded using the call System.loadLibrary("name-of-your-CppJNILibrary").

 

The following IDE features are supported for the associated Java and native projects.

Feature Description
Navigating in the code editor from Java methods to their C/C++ implementation Ctrl-click on the Java method and the code editor jumps to the C/C+ declaration or definition if it is found.
Generating boilerplate code to call Java methods or access Java fields from the native side. Use the context menu to automatically generate code. Right-click a Java entity and choose Mixed Development > Copy JNI Signature.  The signature of the entity is copied into your clipboard and you can paste it where appropriate.

The same works for boilerplate code to access (set or get) Java fields or call Java methods. Right-click on a Java field or method, then select Mixed Development > Copy call method code / Copy get field code / Copy set field code. The requested code is then copied into the clipboard.

Regenerating JNI header files if interface in Java classes has been changed. Right-click on the Java class, then select Mixed Development >  Generate JNI header
If a header file for that class exists in the open C++ projects, the IDE regenerates that existing header file.
If a header file for that class doesn't exist in the open C++ projects, the IDE asks you to choose a directory for the new header file. The IDE generates both the header and '.cpp' files in the project. The C++ file contains empty implementations of all functions.
Debugging both Java and C++ code at once. If you run into a native method call while debugging Java code, you can step into it. The IDE will automatically attach the debugger and place a breakpoint in the beginning of the native method.

 

 


Copyright © 2015, Oracle and/or its affiliates. All rights reserved.