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?