# Gradle プロジェクトで JNI 開発をセットアップする

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/ja-jp/help/idea/llms.txt).

IntelliJ IDEA は [Gradle の JNI 開発(英語)](https://docs.gradle.org/current/userguide/native_software.html)プロジェクトをサポートします。

このチュートリアルでは、Java 1.8 および Gradle 7.4 バージョンを使用します。 チュートリアルでは、 [ソフトウェアモデル(英語)](https://blog.gradle.org/state-and-future-of-the-gradle-software-model)を使用します。

[代替プラグイン(英語)](https://docs.gradle.org/current/userguide/building_cpp_projects.html#cpp_plugin)を使用する場合は、 [JNI サンプル(英語)](https://github.com/vladsoroka/GradleJniSample)プロジェクトを確認できます。

> **Note:**
> プロジェクトで Kotlin を使用している場合は、 [Kotlin チュートリアル(英語)](https://kotlinlang.org/docs/get-started-with-jvm-gradle-project.html)を確認してください。

Procedure: JNI サポートを追加するには

1. [新規](gradle.html#project_create_gradle)に作成するか、 [既存](gradle.html#gradle_import)の Gradle プロジェクトを開きます。

2. `build.gradle`  ファイルを開きます。

3. 次のコードを  `build.gradle`  に追加します。

Windows で実行するには:

```GROOVY
import org.gradle.internal.jvm.Jvm

plugins {
    id 'java'
    id 'application'
    id 'c'
}

mainClassName = 'HelloWorld'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'junit:junit:4.12'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

application {
    applicationDefaultJvmArgs = ["-Djava.library.path=" + file("${buildDir}/libs/hello/shared").absolutePath]
}

model {
    platforms {
        x64 {
            architecture "x86_64"
        }
    }

    components {
        hello(NativeLibrarySpec) {
            targetPlatform "x64"
            binaries.all {
                def jvmHome = Jvm.current().javaHome
                if (targetPlatform.operatingSystem.macOsX) {
                    cCompiler.args '-I', "${jvmHome}/include"
                    cCompiler.args '-I', "${jvmHome}/include/darwin"
                    cCompiler.args '-mmacosx-version-min=10.4'
                    linker.args '-mmacosx-version-min=10.4'
                } else if (targetPlatform.operatingSystem.linux) {
                    cCompiler.args '-I', "${jvmHome}/include"
                    cCompiler.args '-I', "${jvmHome}/include/linux"
                    cCompiler.args '-D_FILE_OFFSET_BITS=64'
                } else if (targetPlatform.operatingSystem.windows) {
                    cCompiler.args "-I${jvmHome}/include"
                    cCompiler.args "-I${jvmHome}/include/win32"
                } else if (targetPlatform.operatingSystem.freeBSD) {
                    cCompiler.args '-I', "${jvmHome}/include"
                    cCompiler.args '-I', "${jvmHome}/include/freebsd"
                }
            }
        }
    }
}

classes.dependsOn 'helloSharedLibrary'

```

macOS で実行するには:

```GROOVY
import org.gradle.internal.jvm.Jvm

plugins {
    id 'java'
    id 'application'
    id 'c'
}

mainClassName = 'HelloWorld'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'junit:junit:4.12'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

application {
    applicationDefaultJvmArgs = ["-Djava.library.path=" + file("${buildDir}/libs/hello/shared").absolutePath]
}

model {
    platforms {
        x64 {
            architecture "x86_64"
        }
    }

    components {
        hello(NativeLibrarySpec) {
            targetPlatform "x64"
            binaries.all {
                def jvmHome = Jvm.current().javaHome
                if (targetPlatform.operatingSystem.macOsX) {
                    cCompiler.args '-I', "${jvmHome}/include"
                    cCompiler.args '-I', "${jvmHome}/include/darwin"
                    cCompiler.args '-mmacosx-version-min=10.9'
                    linker.args '-mmacosx-version-min=10.9'
                    linker.args '-stdlib=libc++'
                } else if (targetPlatform.operatingSystem.linux) {
                    cCompiler.args '-I', "${jvmHome}/include"
                    cCompiler.args '-I', "${jvmHome}/include/linux"
                    cCompiler.args '-D_FILE_OFFSET_BITS=64'
                } else if (targetPlatform.operatingSystem.windows) {
                    cCompiler.args "-I${jvmHome}/include"
                    cCompiler.args "-I${jvmHome}/include/win32"
                } else if (targetPlatform.operatingSystem.freeBSD) {
                    cCompiler.args '-I', "${jvmHome}/include"
                    cCompiler.args '-I', "${jvmHome}/include/freebsd"
                }
            }
        }
    }
}

classes.dependsOn 'helloSharedLibrary'

```

セットアップをスムーズに行うには、次の点に注意してください:

* IntelliJ IDEA は、 [Gradle 設定](gradle-settings.html#gradle_jvm)にある Gradle JVM のバージョンを使用します。

* OS によってコンパイラーのパラメーターが異なる場合があるため、 `cCompiler.args` と `linker.args` を `build.gradle` 内で変更する際は、その点に注意してください。

* アプリケーションが次の既存のシステムプロパティで実行される場合: ```GROOVY application { applicationDefaultJvmArgs = ["-Djava.library.path=" + file("${buildDir}/libs/hello/shared").absolutePath] } ``` このアプリケーションは `classes.dependsOn 'helloSharedLibrary'` に依存し、IntelliJ IDEA は プロジェクト ツールウィンドウで共有ライブラリのバイナリ `libhello.dylib` ファイルを生成します。 ![共有ライブラリのバイナリ](https://resources.jetbrains.com/help/img/idea/2026.3/binary_for_shared_library.png) > **Note:** > `libhello.dylib` は、使用している OS に依存することに注意してください。 例: Windows の場合、バイナリの名前は `hello.dll` になります。

4. プロジェクト   ツールウィンドウで、  `src | java`  ディレクトリを選択します。

5. `java`  ディレクトリを右クリックし、  `新規 | Java クラス`  を選択して、  `C`  コードを使用する Java クラス  `HelloWorld`  を作成します。

6. 作成された  `HelloWorld`  クラスをエディターで開き、次のコードを入力します。

```JAVA
class HelloWorld {
    public native void print();

    static {
        System.loadLibrary("hello");
    }

    public static void main(String[] args) {
        new HelloWorld().print();
    }
}

```

ファイルで指定されたコードは、生成された [システムライブラリ](#library_binary)をロードします。

7. プロジェクト   ツールウィンドウの  `src`  ディレクトリで、  `C`  プログラム用のファイルである  `hello/c/hello.c`  ファイルを作成します。

8. エディターで  `hello.c`  ファイルを開き、次のコードを指定します。

```CSHARP
#include <jni.h>
#include <stdio.h>

JNIEXPORT void JNICALL
Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
printf("Hello From C++ World!\n");
return;
}

```

この時点で、必要に応じてネイティブコードを使用して、アプリケーションの開発をさらに進めることができます。

Procedure: アプリケーションを実行する

1. 実行構成ダイアログを開きます。

2. オペレーティングシステムとして macOS を使用している場合は、次のコードを   VM オプション   フィールドに追加します。

![実行構成](https://resources.jetbrains.com/help/img/idea/2026.3/run_config_vm_options.png)

VM オプションフィールドは、   オプションを変更   リストから追加できます。

OK   をクリックして変更を保存します。

3. メインツールバーの ![実行ボタン](https://resources.jetbrains.com/help/img/idea/2026.3/app.runConfigurations.testState.run.svg) をクリックします。

