When java 9 module require any third party jar or lib which is not developer in java 9 as module at that time we need to add that jar in module path and write requires statement in module-info.java file.

i.e.

requires gauva.jar

Example:

java-9-module-structure

module-info.java

module javadeveloperzone.base{
   requires guava;
}

 Demo.java

package com.javadeveloperzone;
import com.google.common.base.Optional;
public class Demo{
  public static void main(String ... args){
    Optional b =  Optional.of(new Integer(10));
    System.out.println(b.get());
  }
}

 Command To run module:

mkdir mlib
echo "Compiling Class files of module"
javac --module-path mlib -d output/classes base/com/javadeveloperzone/*.java
javac --module-path mlib -d output/classes base/Module-info.java

echo "Creating javadeveloperzone.base module Jar"

jar -c -f mlib/javadeveloperzone.base.jar -C output/classes .


echo "Running javadeveloperzone.base module..."
java --module-path mlib -m javadeveloperzone.base/com.javadeveloperzone.Demo

 Output:

10

 

Check Dependency:

jdeps --module-path . -s  javadeveloperzone.base.jar

 

module-jdeps

module-jdeps

Limitations:

If jar name contains – or . characters at that time module-info.java does not allowed module name with – or .

i.e

require gauva-11.5

gauva-11.5 is invalid. In this case rename jar file and remove – or . characters from jar file.

Example using maven : how to use third party dependency using maven

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *