How to add a Library in the main module [ module-info.java ]?

Asked

Viewed 229 times

2

I use the jssc package to access serial port. I added it to the main module

module DeskCheck {
  requires java.desktop;
  requires java.logging;
  requires jssc;
}

But I believe I’m doing it wrong, because there’s an error saying that module doesn’t exist.

Creating jlink image in dist\jimage/.
Executing: C:\Program Files\jdk-12.0.2\bin\jlink.exe --module-path dist\DeskCheck.jar;C:\Program Files\jdk-12.0.2\jmods --add-modules DeskCheck --output dist\jimage --compress=2
Error: Module jssc not found, required by DeskCheck

1 answer

2

You’re suffering from the automatic modules.

Automatic modules are named modules that are automatically created for a non-modular JAR. This happens when the JAR is placed in the module-path (dependency) on a modular application.

In other words: Non-modular JAR files become modular when used by a modular application.


Let’s say we have a modular application "app" who wants to use a non-modular JAR lib.jar (has not module-info). To rotate the main of app (com.app.Main.class) we will use the following command:

java --module-path appClasses lib --module app/com.app.Main

In command above, appClasses is the folder where app has its classes and lib is the folder where we have the JAR lib.jar.

Thus lib.jar (that’s in the folder lib) automatically becomes modular to app. This module has the same name as the JAR name (without the extension .jar; hyphens ("-") if it has, are replaced with dots ("." ); the version, if it has, is removed as well). The module app still have to requires the JAR module by its name.


Which packages the automatic modules exports?

Him exports all JAR packages.

What modules he requires?

Him requires all modules in the module-path.


Example

This example will create 2 applications. The first application will represent a third-party library, and the second will be a modular application that will be using the first.

The non-modular library

// matematica-iz/src/matematica/iz/CalculoBasico.java
package matematica.iz;

public class CalculoBasico {
  public static int soma(int a, int b) {
      return a + b;
  }
}
C:\exemplo-modulo-automatico\matematica-iz> tree /F /A
|   
\---src
    \--matematica
        \---iz
                CalculoBasico.java

Compiling the class:

C:\exemplo-modulo-automatico\matematica-iz> javac -d out src/matematica/iz/CalculoBasico.java

Creating the JAR:

C:\exemplo-modulo-automatico> jar -cf matematica-iz.jar -C matematica-iz/out .

The modular application

// mat.app/src/com/exemplo/Main.java
package com.exemplo;

import matematica.iz.CalculoBasico;

public class Main {
  public static void main(String[] args) {
      int soma = CalculoBasico.soma(4, 7);
      System.out.println("O resultado da soma é: " + soma);
  }
}
// mat.app/src/module-info.java
module mat.app {
 requires matematica.iz;
}

Note that the above application requires the JAR module (automatic) by its name, which will become matematica-iz.jar for matematica.iz

C:\exemplo-modulo-automatico\mat.app> tree /F /A
|   
\---src
    |   module-info.java
    |   
    \---com
        \---exemplo
                Main.java

Moving matematica-iz.jar for the application of mat.app:

C:\exemplo-modulo-automatico> mkdir mat.app\lib
C:\exemplo-modulo-automatico> move matematica-iz.jar mat.app\lib\
        1 arquivo(s) movido.

Now, here’s the structure of our directory

C:\exemplo-modulo-automatico> tree /F /A
|           
+---matematica-iz
|   |   
|   +---out
|   |   \---matematica
|   |       \---iz
|   |               CalculoBasico.class
|   |               
|   \---src
|       \---matematica
|           \---iz
|                   CalculoBasico.java
|                   
\---mat.app
    |   
    +---lib
    |       matematica-iz.jar
    |             
    \---src
        |   module-info.java
        |   
        \---com
            \---exemplo
                    Main.java

Compiling the application:

C:\exemplo-modulo-automatico\mat.app> javac --module-path lib -d out src/module-info.java src/com/exemplo/Main.java

Running the application

C:\exemplo-modulo-automatico\mat.app> java --module-path out lib --module mat.app/com.exemplo.Main
O resultado da soma é: 11

Using jdeps

C:\exemplo-modulo-automatico\mat.app> jdeps --module-path lib out -s --module mat.app
mat.app -> matematica.iz
mat.app -> java.base

In addition to this concept, we also have the nameless modules, but this I leave for you to research.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.