0

After creating modular application in NetBeans 11.0, I'm able to start it with syntax:

java -p MODULE_PATH --add-modules MODULES -jar App.jar

I would like to start application with:

java -p MODULE_PATH -m module[/mainclass] or --module modulename[/mainclass]

There are a lot of examples on the internet, but with compile on command line not with NetBeans usage.

Is there a way to start application created in NetBeans with just --module example above, or I need to execute few commands to enable this?

For test I created:
Service module:

module com.test {
exports com.test.interfaces;
uses com.test.interfaces.ITest;}

Service class:


import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;

public interface ITest {

  static List<ITest> getInstances() {
      ServiceLoader<ITest> services = ServiceLoader.load(ITest.class);
      List<ITest> list = new ArrayList<>();
      services.iterator().forEachRemaining(list::add);
      return list;
  }

  void print(String text);}

Service provider module:

module com.test.provider {
  requires com.test;
  provides com.test.interfaces.ITest with com.test.provider.impl.ITestImpl;
}

Service provider class:

package com.test.provider.impl;

import com.test.interfaces.ITest;

public class ITestImpl implements ITest {

  @Override
  public void print(String text) {
      System.out.println("Message: " + text);
  }

}  

Client application module:

module org.test {
  exports org.test.app;
  requires com.test;
  requires com.test.provider;
}

Client application class:

package org.test.app;

import com.test.interfaces.ITest;
import java.util.List;

public class TestServiceClient {

  public static void main(String[] args) {
      List<ITest> list = ITest.getInstances();
      for (ITest object : list) {
          object.print("Hello");
      }
  }

}  

Above is example of service, service provider and client application which could be started with following command:

java -p mods --add-modules com.test,com.test.provider -jar TestServiceClient.jar

But it doesn't work with:

java -p mods -m org.test/org.test.app.TestServiceClient.jar

What I'm doing wrong?

John
  • 49
  • 1
  • 9
  • If your module has the appropriate `requires` directives in the `module-info` file then using `--add-modules` should be unnecessary. – Slaw Jun 13 '19 at 13:47
  • @Slaw I have simple ServiceLoader implementation, and client app that just get instances of service. It requires that service and it is implemented, but I couldn't start application with --module. Just to mention, application works, if I start it with NetBeans or with --add-modules. – John Jun 14 '19 at 10:02
  • The module that loads the service implementations should have a `uses ` in its `module-info` file. The modules that implement (i.e. provide) the service should have a `provides with `. The names are the fully qualified class names. Then everything needs to be on the modulepath. By default, the module system will look at the root module and look for all the `requires` modules (transitively) as well as any modules that provide a needed service. – Slaw Jun 14 '19 at 10:33
  • @Slaw I edited my question with example. – John Jun 14 '19 at 21:52
  • The part after the `/` is not supposed to be the name of the JAR file but rather the fully qualified name of the main class (i.e. `--module /`). – Slaw Jun 14 '19 at 21:57
  • @Slaw I tried without .jar, still same result. – John Jun 14 '19 at 23:06
  • @Slaw I tried: `java -p mods --module org.test/org.test.app.TestServiceClient` `java -p mods --module org.test/TestServiceClient` `java -p mods --module org.test/org.test.app.TestServiceClient` And every time I get: `Error occurred during initialization of boot layer java.lang.module.FindException: Module org.test not found` – John Jun 14 '19 at 23:11
  • @Slaw My client app is normal Java application, where I added module info, is it possible that there is problem? Service i service provider modules are created in Modular Project. I mean it works any way around except with those module command. – John Jun 14 '19 at 23:22

0 Answers0