module-info.java は、「このモジュールは何を使い(requires)、何を外に見せるか(exports・opens)」を書くファイルです。 ソースの一番上(パッケージのフォルダの外)に置きます。JPMS(Java Platform Module System)は、Java 9 で入ったこの仕組みの名前です。

module com.example.greet {
    requires transitive java.sql;          // 使う。利用者にも java.sql を見せる
    requires static java.compiler;         // コンパイル時だけ必須(実行時は任意)

    exports com.example.greet.api;         // 公開するパッケージ
    exports com.example.greet.spi;
    opens com.example.greet.model;         // 実行時のリフレクションだけ許可

    uses com.example.greet.spi.Greeter;    // ServiceLoader で探すサービス
    provides com.example.greet.spi.Greeter
        with com.example.greet.internal.JapaneseGreeter;  // サービスの実装
}
書き方 意味
requires M; モジュール M を使う(読む)
requires transitive M; M を使い、自分を使う側にも M を読ませる
requires static M; コンパイル時は必須、実行時は任意
exports p; パッケージ p の public な型を、全モジュールに公開
exports p to M1, M2; p を M1・M2 にだけ公開
opens p; p を実行時のリフレクション(private も含む)にだけ開く
opens p to M1; p を M1 にだけ開く
open module M { … } モジュールの全パッケージを opens する
uses S; サービス S を ServiceLoader で探す
provides S with Impl; サービス S の実装 Impl を提供する

確認環境:Ubuntu 24.04、OpenJDK 25.0.4.1(主なエラー文は 21.0.10 でも同じ文面でした)、Gradle 8.14.3。上の module-info.java を含む2つのモジュールを javac でコンパイル・実行し、エラー文はすべてその1か所を書き換えて出したものです。書き方の規則は Java 言語仕様(JLS SE 25)の 7.7 節・7.5.5 節で確かめました(確認日 2026年10月13日)。

本記事の内容は、ご自由にお使いください。
ご利用の際は、出典として本ページへのリンクを記載いただけますようお願いします。

(記載例)出典:株式会社RJC「module-info.javaの書き方|requires・exports一覧」

まず全体の形:フォルダとコマンド

2つのモジュールを作ります。com.example.greet(ライブラリ側)と、それを使う com.example.app(アプリ側)です。

src/
├── com.example.greet/
│   ├── module-info.java           ← モジュール名のフォルダの直下
│   └── com/example/greet/
│       ├── api/Greetings.java     ← exports(公開)
│       ├── spi/Greeter.java       ← exports(サービスのインターフェース)
│       ├── model/User.java        ← opens(リフレクションだけ)
│       └── internal/JapaneseGreeter.java  ← 非公開
└── com.example.app/
    ├── module-info.java
    └── com/example/app/Main.java

ライブラリ側のコード

// com/example/greet/spi/Greeter.java
package com.example.greet.spi;

public interface Greeter {
    String greet(String name);
}
// com/example/greet/internal/JapaneseGreeter.java
package com.example.greet.internal;

import com.example.greet.spi.Greeter;

public class JapaneseGreeter implements Greeter {
    public String greet(String name) { return "こんにちは、" + name + "さん"; }
}
// com/example/greet/api/Greetings.java
package com.example.greet.api;

import com.example.greet.spi.Greeter;
import java.sql.Timestamp;
import java.util.ServiceLoader;

public class Greetings {
    public static String hello(String name) {
        return ServiceLoader.load(Greeter.class).findFirst()
                .map(g -> g.greet(name)).orElse("Hello, " + name);
    }
    public static Timestamp now() {     // java.sql の型を公開 API で返す
        return new Timestamp(System.currentTimeMillis());
    }
}
// com/example/greet/model/User.java
package com.example.greet.model;

public class User {
    private String name = "RJC";
}

アプリ側のコード

// com.example.app/module-info.java
module com.example.app {
    requires com.example.greet;
}
// com/example/app/Main.java
package com.example.app;

import com.example.greet.api.Greetings;
import java.sql.Timestamp;   // requires transitive のおかげで、requires java.sql を書かずに使える

public class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Greetings.hello("たかひら"));
        Timestamp t = Greetings.now();
        System.out.println("Timestamp class from " + t.getClass().getModule());

        var f = Class.forName("com.example.greet.model.User").getDeclaredField("name");
        f.setAccessible(true);   // opens しているので private でも読める
        System.out.println("reflection: " + f.get(f.getDeclaringClass().getDeclaredConstructor().newInstance()));
    }
}

コンパイルと実行

javac -d out --module-source-path src -m com.example.app,com.example.greet
java -p out -m com.example.app/com.example.app.Main
こんにちは、たかひらさん
Timestamp class from module java.sql
reflection: RJC
オプション 意味
--module-source-path src src/モジュール名/ の形で複数モジュールをまとめてコンパイル
-m モジュール名,…(javac) コンパイルするモジュール
-p(--module-path) モジュールの置き場所。jar やフォルダ
-m モジュール/クラス(java) 起動するモジュールとメインクラス

モジュールの中身は --describe-module で確かめられます。書いたとおりに読み取られ、requires java.base mandated(自動で付く)と、公開していない contains com.example.greet.internal も表示されました。

$ java -p out --describe-module com.example.greet
com.example.greet file:///…/out/com.example.greet/
exports com.example.greet.api
exports com.example.greet.spi
requires java.base mandated
requires java.compiler static
requires java.sql transitive
uses com.example.greet.spi.Greeter
provides com.example.greet.spi.Greeter with com.example.greet.internal.JapaneseGreeter
opens com.example.greet.model
contains com.example.greet.internal

requires の書き方

requires と java.base

java.base(String・List などの基本)は、書かなくても自動で読まれます。 JLS では「java.base への依存を書かなければ、暗黙に宣言される」とされています。それ以外の JDK のモジュール(java.sql・java.net.http など)は、使うなら requires が要ります。

requires transitive:自分の API に出てくる型のモジュール

ライブラリの公開メソッドが java.sql.Timestamp を返すなら、使う側も java.sql を読めないとその型を扱えません。requires transitive java.sql; と書くと、com.example.greet を requires した側は、自動で java.sql も読めます。

transitive を外して requires java.sql; にすると、アプリ側の import java.sql.Timestamp; がエラーになりました。

error: package java.sql is not visible
  (package java.sql is declared in module java.sql, but module com.example.app does not read it)

-Xlint:exports を付けてライブラリ側をコンパイルすると、この漏れを警告で教えてくれました。

warning: [exports] class Timestamp in module java.sql is not indirectly exported using 'requires transitive'

requires static:あれば使う、任意の依存

requires static は「コンパイル時は必須、実行時は任意」(JLS)です。アノテーションだけのライブラリや、入っていれば使う機能に使います。

自作の com.example.opt を requires static して試すと、モジュールパスに置いてあるだけでは読み込まれませんでした。

module com.example.core {
    requires static com.example.opt;
}
boolean has = Main.class.getModule().getLayer()
        .findModule("com.example.opt").isPresent();
if (has) {
    System.out.println(com.example.opt.Metrics.name());
}
実行のしかた 結果
java -p out -m com.example.core/…(opt もパスにある) opt module: false(読み込まれない)
java -p out --add-modules com.example.opt -m … opt module: true
opt が無いのに確かめずに呼ぶ NoClassDefFoundError: com/example/opt/Metrics

requires static のモジュールを使うには、--add-modules で足すか、他のモジュールが普通に requires している必要があります。 呼ぶ前に findModule で有無を確かめる書き方が安全です。

exports と opens の違い

コンパイル時に使える public への通常アクセス private へのリフレクション
何も書かない × × ×
exports p; ○ ○ ×
opens p; × ○(リフレクションのみ) ○
exports p; + opens p; ○ ○ ○

exports しても、private フィールドへの setAccessible(true) は止まりました。 opens を exports に書き換えて実行した結果です。

java.lang.reflect.InaccessibleObjectException: Unable to make field private java.lang.String com.example.greet.model.User.name accessible: module com.example.greet does not "opens com.example.greet.model" to module com.example.app

JSON の変換(Jackson・Gson)や JPA(Hibernate)のように、リフレクションで private を読み書きするライブラリに渡すクラスのパッケージは、opens が要ります。 相手を絞るなら opens com.example.greet.model to com.fasterxml.jackson.databind; のように書きます。

自分で直せないモジュールは、実行時のオプションで開けます。opens を消した状態で次を付けると、reflection: RJC と表示されました。

java --add-opens com.example.greet/com.example.greet.model=com.example.app \
     -p out -m com.example.app/com.example.app.Main

JDK のクラスで同じエラーが出たときの直し方は、関連記事「InaccessibleObjectExceptionの直し方|Java 17」で扱っています。

open module:全部を opens する

open module com.example.greet { … } と書くと、全パッケージが opens されます。このとき中に opens を書くとエラーになりました。

error: 'opens' only allowed in strong modules

uses と provides:ServiceLoader

provides サービス with 実装; で実装を登録し、ServiceLoader を呼ぶ側のモジュールに uses サービス; を書きます。 実装クラスのパッケージ(上の例では internal)は exports しなくても、ServiceLoader から使えました。

uses を消すと、コンパイルは通り、実行時に止まりました。

java.util.ServiceConfigurationError: com.example.greet.spi.Greeter: module com.example.greet does not declare `uses`

uses は「ServiceLoader.load を呼んでいるコードがあるモジュール」に書きます。 実装を持つモジュールではない点に注意します。

よく出るエラー一覧(JDK 25 で再現)

エラー文(要点) 原因 直し方
package X is not visible + which does not export it 相手が X を exports していない 相手に exports X;(内部用なら公開しない)
package X is not visible + does not read it 自分が相手を requires していない requires 相手;
… which does not export it to module 自分 exports X to 他; で自分が入っていない to に自分を足す
… declared in the unnamed module, but module … does not read it ライブラリをクラスパスに置いた モジュールパス(-p)に置き、requires する
module not found: M M がモジュールパスに無い・名前違い -p に M の jar を足す
FindException: Module M not found, required by …(実行時) 実行時の -p に M が無い -p に足す
cyclic dependence involving M モジュール同士が互いに requires 共通部分を別モジュールに分ける
package exists in another module: M 同じパッケージ名が2つのモジュールにある パッケージ名を分ける
reads package X from both M1 and M2 requires した2つが同じパッケージを持つ どちらかに寄せる
LayerInstantiationException: Package X in both module …(実行時) 同じパッケージのモジュールが2つ読み込まれた 片方を外す
InaccessibleObjectException … does not "opens X" リフレクションで private に触れた opens X; または --add-opens
ServiceConfigurationError … does not declare \uses`|usesが無い | 呼ぶ側にuses S;`
package is empty or does not exist: X exports したパッケージにクラスが無い パッケージ名を直す
module name A does not match expected name B フォルダ名とモジュール名が違う src/モジュール名/ に合わせる

ほかに、同じ行を2回書くと duplicate requires: java.sql・duplicate or conflicting exports: p になりました。package exists in another module: java.base は、自分のモジュールに package java.util; を書いたときに出ました。

実際のエラー文(全文)

src/com.example.app/com/example/app/Main.java:5: error: package com.example.greet.internal is not visible
import com.example.greet.internal.JapaneseGreeter;
                        ^
  (package com.example.greet.internal is declared in module com.example.greet, which does not export it)
src/com.example.app/com/example/app/Main.java:3: error: package com.example.greet.api is not visible
import com.example.greet.api.Greetings;
                        ^
  (package com.example.greet.api is declared in module com.example.greet, but module com.example.app does not read it)
src/com.example.app/module-info.java:2: error: cyclic dependence involving com.example.greet
    requires com.example.greet;
                        ^
Error occurred during initialization of boot layer
java.lang.module.FindException: Module com.example.greet not found, required by com.example.app

which does not export it(相手の exports の問題)と does not read it(自分の requires の問題)で、直すファイルが違う点が見分けどころです。コンパイルエラー全般は、関連記事「Javaコンパイルエラー一覧|cannot find symbol他20種と対処法」で扱っています。

どうしても内部パッケージを使うとき

テストや移行の途中で、exports されていないパッケージを使うときは --add-exports があります。上の「does not export it」の状態でも、次でコンパイルが通りました。実行時にも同じオプションを付けます。

javac --add-exports com.example.greet/com.example.greet.internal=com.example.app \
      -d out --module-source-path src -m com.example.app,com.example.greet

module-info の無い jar を使う(自動モジュール)

module-info の無い jar をモジュールパスに置くと「自動モジュール」になり、名前は jar の Automatic-Module-Name(MANIFEST)か、ファイル名から決まります。

jar ファイル名 モジュール名
my-util-1.2.3.jar my.util(版は @1.2.3)
commons-lang3-3.17.0.jar commons.lang3
acme-util-9.jar(MANIFEST に Automatic-Module-Name: com.acme.util) com.acme.util
foo-default-1.0.jar エラー:'default' is not a Java identifier
2fa-lib-1.0.jar エラー:'2fa' is not a Java identifier

名前は jar --describe-module --file my-util-1.2.3.jar で確かめられます。名前を作れない jar は次のエラーになりました。

Unable to derive module descriptor for: lib/foo-default-1.0.jar
foo.default: Invalid module name: 'default' is not a Java identifier

自動モジュールを requires すると、-Xlint:requires-automatic で警告が出ました。ファイル名から作った名前は、ライブラリ側が後で正式な名前を付けると変わるためです。

warning: [requires-automatic] requires directive for an automatic module

同じ jar をクラスパス(-cp)に置くと、モジュールからは読めません。 実行時は FindException: Module my.util not found、コンパイル時は declared in the unnamed module, but module … does not read it になりました。

Gradle・Maven での書き方

どちらも src/main/java/module-info.java に置けば、モジュールとしてビルドされます。 --module-source-path のようなモジュール名のフォルダは作りません。

// app/build.gradle.kts
plugins { application }
java { toolchain { languageVersion = JavaLanguageVersion.of(21) } }
dependencies { implementation(project(":greet")) }
application {
    mainModule = "com.example.app"
    mainClass = "com.example.app.Main"
}

この形で gradle :app:run を動かすと、上と同じ3行が表示されました。Gradle のドキュメントでは、module-info.java があるとき、依存の jar のうち module-info か Automatic-Module-Name を持つものはモジュールパスに、どちらも無い jar はクラスパスに置く、とされています。

Gradle 8.14.3 で toolchain を 25 にすると、Unsupported class file major version 69 で止まりました。Java 25 でビルドするときは、Gradle の対応版を確かめます。build.gradle.kts の書き方は、関連記事「build.gradle.kts書き方|Groovy対応表とバージョンカタログ」で扱っています。

Maven も、src/main/java/module-info.java を置く形です。maven-compiler-plugin のドキュメントには、Java 8 向けにビルドしつつ module-info だけ Java 9 以上でコンパイルする、2回コンパイルの設定例が載っています。pom.xml の書き方は、関連記事「Mavenコマンド一覧|pom.xml書き方とよく使うオプション」で扱っています。

Java 25 の import module(モジュール単位の import)

Java 25 では import module java.sql; で、そのモジュールが exports する全パッケージを import できます(JEP 511)。 module-info.java を書かない普通のクラスでも使えます。

import module java.sql;
import module java.base;
import java.sql.Timestamp;   // 個別の import が優先される

public class Demo {
    public static void main(String[] args) {
        Timestamp ts = Timestamp.valueOf("2026-10-13 09:00:00");
        List<String> names = List.of("a", "b");
        System.out.println(ts + " " + names);
    }
}
2026-10-13 09:00:00.0 [a, b]

動かして分かった注意点です。

書き方 結果
import module java.sql; だけ List が cannot find symbol(java.base は入らない)
java.sql と java.base を両方 reference to Timestamp is ambiguous(java.security.Timestamp と衝突)
上に import java.sql.Timestamp; を足す 動いた
java.base と java.desktop を両方 reference to List is ambiguous(java.awt.List と衝突)
JDK 21 でコンパイル error: '.' expected

JLS 7.5.5 節では、import module で入るのは「そのモジュールが exports するパッケージ」と「そのモジュールを通して(requires transitive で)読むモジュールのパッケージ」です。java.base は transitive ではないので入りません。 一方、java.sql が transitive に読む java.xml の org.w3c.dom.Document は、import module java.sql; だけで使えました。

よくある質問

Q. module-info.java は必ず書くものですか。 A. 書かなければ、これまでどおりクラスパスで動きます(無名モジュール)。書くと、公開範囲を exports で絞れる、jlink で最小のランタイムを作れる、といった利点があります。jlink は、関連記事「jlink・jpackageの使い方|最小JREとインストーラ作成」で扱っています。

Q. モジュール名はどう付けますか。 A. 自作のモジュールは、パッケージと同じ逆ドメイン形式(com.example.greet)がよく使われます。モジュールの主なパッケージ名と揃えると、他のモジュールと名前が重なりにくくなります。名前に default などの予約語や数字で始まる部分は使えません。

Q. パッケージと import の基本は? A. 関連記事「Javaのpackageとimportの使い方|クラスパスの通し方」で扱っています。

まとめ

  • module-info.java は src/モジュール名/ 直下(Gradle・Maven は src/main/java/ 直下)
  • 公開 API に出てくる型のモジュールは requires transitive。-Xlint:exports で漏れが分かる
  • requires static は、パスにあるだけでは読み込まれない。--add-modules か有無の確認
  • exports はコンパイル時と public、opens は実行時のリフレクション。private を触るなら opens
  • uses は ServiceLoader を呼ぶ側に書く
  • エラーは does not export it(相手を直す)と does not read it(自分を直す) を見分ける

参考・出典

確認日はいずれも 2026年10月13日です。

  • Java 言語仕様 SE 25「Chapter 7. Packages and Modules」(7.5.5・7.7):https://docs.oracle.com/javase/specs/jls/se25/html/jls-7.html
  • OpenJDK「JEP 261: Module System」:https://openjdk.org/jeps/261
  • OpenJDK「JEP 511: Module Import Declarations」:https://openjdk.org/jeps/511
  • Gradle「The Java Library Plugin」(Building Modules for the Java Module System):https://docs.gradle.org/current/userguide/java_library_plugin.html
  • Apache Maven Compiler Plugin「Older projects with module-info」:https://maven.apache.org/plugins/maven-compiler-plugin/examples/module-info.html

本記事の内容は、ご自由にお使いください。
ご利用の際は、出典として本ページへのリンクを記載いただけますようお願いします。

(記載例)出典:株式会社RJC「module-info.javaの書き方|requires・exports一覧」

株式会社RJC ― SI事業・SES事業・AI駆動開発。RJCは一緒に成長を楽しめる会社です。

WE ARE HIRING

RJCで一緒に開発しながら、
成長を楽しみませんか?

RJCは、Web・モバイル・AIを活用した開発プロジェクトで、テックリードやPM・PMOも活躍するシステム開発会社です。会社を知る、待遇を確かめる、話を聞いてみる。気になるところ見てみてください!

  • 127日年間休日
  • 12時間平均残業時間
  • 毎日ガチャ遊びココロも大切にする福利厚生。アマギフなどの賞品ラインナップ!

ほかにも、チケットレストラン、書籍読み放題、2年ごとの慰労報奨(休暇 or 金一封)、11期連続の黒字決算。

ABOUT RJC RJCがどんな会社か知る 考え方、研修、働き方、福利厚生、社員の前職まで。RJCのことが丸わかり! RJC丸わかりページへ JOB DESCRIPTION 仕事内容・待遇を見てみる 仕事内容、給与・待遇、選考の流れ。経験者も未経験も!応募前に知りたいこと、まとめました! 募集要項を見る ENTRY エントリーする エントリーは1〜2分・履歴書不要です。まずは話を聞いてみたい、という方でも歓迎です! エントリーフォームへ

成長のヒミツ、 ぜんぶ丸わかり