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.
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.
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.
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
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 (...)of
gyunyu.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.
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 in
Main.java`, the compilation will pass.
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
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