[Java] Module

What is a module?

Traditional packaging issues

Access authority cannot be set for each package

.jar file cannot represent dependencies

Solved by using modules!

Module basic

module-info.java


//Module name foo
//java in foo module.net.need http
module foo {
  requires java.net.http;
  requires mylib;
  requires gson;
}

Standard library module

Basic module

Transitive dependence

module-info.java


module java.se { 
  requires transitive java.compiler;
  requires transitive java.datatranfer;
  requires transitive java.desktop;
//Abbreviation
}

Package release

module-info.java


module mylib { 
  exports mylib.lib;
}
//OK
import mylib.lib.MainLib;
//NG! interna;Package is invisible
//import mylib.internal.Sublib;

public class ModuleClient {

  public static void main(String[] args) {
    var main = new MainLib();
    main.run();
  }
}
module foo {
  requires java.net.http;
  requires mylib;
  requires gson;
}

Package release only for specific modules

module-info.java


module mylib {
  exports mylib.lib to foo;
}

open private method

import java.lang.reflect.InvocationTargetException;
import mylib.lib.MainLib;

public class ModuleClient2 {

  public static void main(String[] args) {
    try {
      var clazz = MainLib.class;
      var con = clazz.getConstructor();
      var m = con.newInstance();
      var name = clazz.getDeclaredField("name"); 
      name.setAccessible(true); //error
      System.out.println(name.get(m));

    } catch (InstantiationException | IllegalAccessException |
        IllegalArgumentException | InvocationTargetException
        | NoSuchMethodException | SecurityException | NoSuchFieldException e) {
      e.printStackTrace();
    }
  }
}

Declare the package with the ** opens directive ** and solve it!

module-info.java


module mylib {
  opens mylib.lib;
  exports mylib.lib;
}

Special module

Automatic module

Determined from manifest information

MANIFEST.MF


Manifest-Version: 1.0
Automatic-Module-Name: hoge.bar

Determined by .jar file name

Anonymous module

Coexistence with non-module libraries

//NG example Run-time error
import com.google.gson.Gson;

public class NoModuleLib {

  public static void main(String[] args) {
    var g = new Gson();
    var a = new Article("Java 11 changes", "https://codezine.jp/article/corner/751");

    //Output the result of converting the object contents to JSON
    System.out.println(g.toJson(a));
  }
}

Article.java


public class Article {
  private String title;
  private String url;

  public Article(String title, String url) {
    this.title = title;
    this.url = url;
  }

  @Override
  public String toString() {
    return String.format("title:%s(%s)",
        this.title, this.url);
  }
}

Explicitly add module with execution option

Recommended Posts

[Java] Module
Java
Java
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
[Java] Overload
Java basics
Decompile Java
[Java] Annotation
java notes
java beginner
Java (add2)
JAVA (Map)
[java] interface
Java9 collection
Java basics