IntelliJ IDEA 2021.2 Help

Set up JNI development in Gradle project

IntelliJ IDEA supports JNI development in Gradle projects.

This tutorial is based on the Java 1.8 and Gradle 4.0 versions. The tutorial uses the software model and gcj sample project.

If you want to use the replacement plugins, you can check the JNI Sample project.

To add JNI support

You can clone and check our the whole JNI project from VCS to see how everything works. Alternatively, you can copy code from the build.gradle file and add it to your project.

  1. Create a new or open an existing Gradle project.

  2. Open the build.gradle file.

  3. Add the following code to the build.gradle:

    apply plugin: 'java' apply plugin: 'application' apply plugin: 'c' mainClassName = 'HelloWorld' repositories { mavenCentral() } dependencies { testCompile('junit:junit:4.12') } sourceCompatibility = 1.8 targetCompatibility = 1.8 test { systemProperty "java.library.path", file("${buildDir}/libs/hello/shared").absolutePath } model { platforms { x64 { architecture "x64" } } components { hello(NativeLibrarySpec) { binaries.all { if (targetPlatform.operatingSystem.macOsX) { cCompiler.args '-I', "${org.gradle.internal.jvm.Jvm.current().javaHome}/include" cCompiler.args '-I', "${org.gradle.internal.jvm.Jvm.current().javaHome}/include/darwin" cCompiler.args '-mmacosx-version-min=10.4' linker.args '-mmacosx-version-min=10.4' } else if (targetPlatform.operatingSystem.linux) { cCompiler.args '-I', "${org.gradle.internal.jvm.Jvm.current().javaHome}/include" cCompiler.args '-I', "${org.gradle.internal.jvm.Jvm.current().javaHome}/include/linux" cCompiler.args '-D_FILE_OFFSET_BITS=64' } else if (targetPlatform.operatingSystem.windows) { cCompiler.args "-I${org.gradle.internal.jvm.Jvm.current().javaHome}/include" cCompiler.args "-I${org.gradle.internal.jvm.Jvm.current().javaHome}/include/win32" linker.args "Shlwapi.lib", "Advapi32.lib" } else if (targetPlatform.operatingSystem.freeBSD) { cCompiler.args '-I', "${org.gradle.internal.jvm.Jvm.current().javaHome}/include" cCompiler.args '-I', "${org.gradle.internal.jvm.Jvm.current().javaHome}/include/freebsd" } } } } } test.dependsOn 'helloSharedLibrary'

    To make your setup work smoothly, keep in mind the following details :

    • IntelliJ IDEA uses the version of Gradle JVM located in the Gradle settings.

    • Depending on your OS, compiler parameters may vary, keep that in mind when you change cCompiler.args and linker.args in your build.gradle.

    • When tests are run with the existing system property test {systemProperty "java.library.path", file("${buildDir}/libs/hello/shared").absolutePath} and those tests depend on test.dependsOn 'helloSharedLibrary', IntelliJ IDEA generates the binary libhello.dylib file for the shared library in the Project tool window.

      binary for shared library
  4. In the Project tool window, select the src | java directory.

  5. Right-click the java directory and select New | Java Class to create a Java class HelloWorld that will use C code.

  6. Open the created HelloWorld class in the editor and enter the following code:

    class HelloWorld { public native void print(); static { System.loadLibrary("hello"); } }

    Code specified in the file will load the generated system library.

  7. In the Project tool window, in the src directory, create the hello directory and the c subdirectory.

  8. In the c subdirectory, create the hello.c file which is a file for your C programs.

    Project tool window, hello.c file
  9. Open the hello.c file in the editor and specify the following code:

    #include <jni.h> #include <stdio.h> JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv *env, jobject obj) { printf("Hello World!\n"); return; }

At this point you can start developing your application further using native codes as needed.

Last modified: 02 August 2022