Saturday, November 25, 2017

Java 9 modules unveiled

Presentation by Mark Reinhold - author of most documentation in openJDK http://openjdk.java.net/projects/jigsaw/





On CentOS 7, to install Java 9:


wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/9.0.1+11/jdk-9.0.1_linux-x64_bin.rpm"

sudo yum install jdk-9.0.1_linux-x64_bin.rpm

rm jdk-9.0.1_linux-x64_bin.rpm

at this point, java9 is installed in /usr/java/jdk-9.0.1/bin/java






./jshell
3 + 4
String x = "foo"
x.substring(1,3)
x.getClass()
x.getClass().getModule()
x.getClass().getModule().getClass()
import java.sql.*;
Timestamp t = new Timestamp(0)
t.getClass()
t.getClass().getModule()
CTRL-D to exit


if you type
./java -help

you get some new options related to modules:



java [options] -m [/] [args...]
java [options] --module [/] [args...]
(to execute the main class in a module)

Arguments following the main class, -jar , -m or --module
/ are passed as the arguments to main class.

-p
--module-path ...
A : separated list of directories, each directory
is a directory of modules.
--upgrade-module-path ...
A : separated list of directories, each directory
is a directory of modules that replace upgradeable
modules in the runtime image
--add-modules [,...]
root modules to resolve in addition to the initial module.
can also be ALL-DEFAULT, ALL-SYSTEM,
ALL-MODULE-PATH.
--list-modules
list observable modules and exit
-d
--describe-module
describe a module and exit
--dry-run create VM and load main class but do not execute main method.
The --dry-run option may be useful for validating the
command-line options such as the module system configuration.
--validate-modules
validate all modules and exit
The --validate-modules option may be useful for finding
conflicts and other errors with modules on the module path.







mkdir -p src/org/openjdk/hello
vi org/openjdk/hello/Main.java


package org.openjdk.hello;
public class Main {
public static void main(String[] args) {
System.out.println("ciao");
}
}



export PATH=/usr/java/jdk-9.0.1/bin/:$PATH
javac -d classes src/org/openjdk/hello/Main.java
java -cp classes/ org.openjdk.hello.Main
mkdir lib
jar --create --file lib/hello.jar -C classes .
ls lib
java -cp lib/hello.jar org.openjdk.hello.Main

vi src/module-info.java

module org.openjdk.hello {
}


javac -d classes src/org/openjdk/hello/Main.java src/module-info.java
ls classes
jar --create --file lib/hello.jar -C classes .
jar tf lib/hello.jar

javap classes/module-info.class

Compiled from "module-info.java"
module org.openjdk.hello {
requires java.base;
}


java --module-path lib -m org.openjdk.hello/org.openjdk.hello.Main

java --module-path lib --describe-module org.openjdk.hello

org.openjdk.hello file:///home/centos/java9code/lib/hello.jar
requires java.base mandated
contains org.openjdk.hello


jar --create --file lib/hello.jar --main-class org.openjdk.hello.Main -C classes .
java --module-path lib -m org.openjdk.hello

rm -rf classes/
mv src org.openjdk.hello
mkdir src
mv org.openjdk.hello src/

I AM GIVING UP HERE!

java --list-modules



Here the specifications http://openjdk.java.net/projects/jigsaw/spec/sotms/

Interesting reading about module vs jar : https://softwareengineering.stackexchange.com/a/313545 by Neil Bartlett

https://stackoverflow.com/a/46514067/651288 also interesting


https://en.wikipedia.org/wiki/Java_Platform_Module_System







No comments: