What I researched about Java 9

Overview

It seems that Java 10 (Is it 18.3?) Will appear soon, so I have described what I investigated and tried with the function added in 9.

What I looked up

JShell JShell was introduced. You can execute the code interactively from the command line. When you want to execute a simple code, you can save the trouble of executing it by using a class file.

helloworld.jpg

List.of、Set.of、Map.of A factory method has been added to the collection class.

List<String> list = List.of("coffee milk", "Strawberry milk");
Set<String> set = Set.of("Lemon milk", "Melon milk");
Map<String, String> map = Map.of("coffee milk", "100 yen", "Strawberry milk", "120 yen");

The collection created by the of method is immutable.

Module function

With the introduction of the module function, it has become possible to clarify the scope of disclosure and dependencies of modules. Public methods such as libraries were referenced in classes that were not supposed to be used ... You can also prevent such things. Reference: Project Jigsaw

When not using the module function

Folder structure of the called side (jar) (1)


gyunyu/
 ┗━ src/
    ┗━ jp/gr/java_conf/masakado/gyunyu/
       ┣━ Coffee.java
       ┗━ util/
          ┗━ Utility.java

Coffee.java


public class Coffee {
	public void print() {
		Utility.output("coffee");
	}
}

java.Utility.java


public class Utility {
	public static void output(String name) {
		System.out.println("this is," + name + "It's milk.");
	}
}

ʻUtility.output (...)` is supposed to be used only in the gyunyu project, This method is not supposed to be executed from the outside.

Compile the above two files to create gyunyu.jar.

javac -d bin -encoding UTF-8 src\jp\gr\java_conf\masakado\gyunyu\Coffee.java src\jp\gr\java_conf\masakado\gyunyu\util\Utility.java
jar -cvf gyunyu.jar -C bin .

Caller's folder structure (1)


sample/
 ┣━ src/
 ┃  ┗━ jp/gr/java_conf/masakado/sample/
 ┃     ┗━ Main.java
 ┗━ lib/
    ┗━ gyunyu.jar

Main.java


import jp.gr.java_conf.masakado.gyunyu.Coffee;
import jp.gr.java_conf.masakado.gyunyu.util.Utility;
public class Main {
	public static void main(String[] args) {
		(new Main()).execute();
	}

	public void execute() {
		Coffee coffee = new Coffee();
		coffee.print();
		Utility.output("Unexpected call: Strawberry"); //Refer to a method that is not supposed to be executed from the outside
	}
}

It refers to ʻUtility.output (...)ofgyunyu.jar` which is not supposed to be executed from the outside. If you compile and run this

javac -d bin -cp lib\gyunyu.jar -encoding UTF-8  src\jp\gr\java_conf\masakado\sample\Main.java
java -cp lib\gyunyu.jar;bin jp.gr.java_conf.masakado.sample.Main

It will be displayed as below.

sample01.jpg

When using the module function

Create module-info.java on the called side (jar).

Folder structure of the called side (jar) (2)


gyunyu/
 ┗━ src/
    ┣━ jp/gr/java_conf/masakado/gyunyu/
    ┃  ┣━ Coffee.java
    ┃  ┗━ util/
    ┃     ┗━ Utility.java
    ┗━ module-info.java

module-info.java


module jp.gr.java_conf.masakado.gyunyu {
	exports jp.gr.java_conf.masakado.gyunyu;
}

ʻExports`: Specify the package to be published to the outside.

The module name is the same as the package name, but you can name it freely without having to match it with the package name. Compile this to create a jar file.

javac -d bin -encoding UTF-8 src\module-info.java src\jp\gr\java_conf\masakado\gyunyu\Coffee.java src\jp\gr\java_conf\masakado\gyunyu\util\Utility.java
jar -cvf gyunyu.jar -C bin .

Place the created gyunyu.jar in sample / lib and create module-info.java.

Caller's folder structure (1)


sample/
 ┣━ src/
 ┃  ┣━ jp/gr/java_conf/masakado/sample/
 ┃  ┃  ┗━ Main.java
 ┃  ┗━ module-info.java
 ┗━ lib/
    ┗━ gyunyu.jar

module-info.java


module jp.gr.java_conf.masakado.sample {
	requires jp.gr.java_conf.masakado.gyunyu;
}

requires: Specify the dependent (used) module.

Compile by specifying gyunyu.jar with the -p option.

javac -d bin -p lib\gyunyu.jar -encoding UTF-8 src\module-info.java src\jp\gr\java_conf\masakado\sample\Main.java

The following error message is output and compilation fails.

Package jp.gr.java_conf.masakado.gyunyu.util cannot be displayed
(Package jp.gr.java_conf.masakado.gyunyu.util is a module jp.gr.java_conf.masakado.Declared by gyunyu but not exported)

If you delete ʻUtility.output (...)described inMain.java`, the compilation will pass.

Display module information

You can refer to the module information by executing the jar command with the -d option specified as shown below.

jar -d -f lib\gyunyu.jar
exports jp.gr.java_conf.masakado.gyunyu
requires java.base mandated
contains jp.gr.java_conf.masakado.gyunyu.util

ʻExports: Published package requires: Dependent modules contains`: Packages that are not listed in exports but are included in the jar

Finally

The introduction of the module function made it easier to understand the dependencies, so when I tried to execute it because the compilation went well, the JAR was not enough and an error occurred ... I felt that the possibility of something like that happening would decrease. I haven't dealt with module-enabled JARs yet, so it may be a long time ago that I can benefit from module functionality.

Recommended Posts

What I researched about Java 8
What I researched about Java 9
What I researched about Java 7
What I researched about Java 5
What I researched about Java learning
What I learned about Kotlin
What I learned with Java Gold
What I learned with Java Silver
About Java interface
[Java] About Java 12 features
What is java
[Java] About arrays
Take what you've learned about Java reflection
Something about java
Where about java
About Java features
What is Java <>?
About Java threads
[Java] About interface
What is Java
What i learned
About Java class
About Java arrays
About java inheritance
About interface, java interface
What I learned from Java monetary calculation
About List [Java]
About java var
About Java literals
About Java commands
What I thought about when I started migrating from Java to Kotlin
Summary of what I learned about Spring Boot
What I learned in Java (Part 2) What are variables?
What I did when I converted java to Kotlin
About Java log output
About Java functional interface
Java, about 2D arrays
About class division (Java)
About [Java] [StreamAPI] allMatch ()
About Java StringBuilder class
I first touched Java ②
[Java] About Singleton Class
I first touched Java ③
About Java method binding
I first touched Java ④
[Java] About anonymous classes
About method splitting (Java)
What is Java Encapsulation?
[Java Silver] About initialization
What I learned ② ~ Mock ~
About Java Array List
About Java Polymorphism super ()
What I learned ① ~ DJUnit ~
About inheritance (Java Silver)
About Java String class
About Java access modifiers
About Java lambda expressions
What is Java technology?
I first touched Java
About Java entry points
About Java 10 Docker support