2

I have a small java 11 module project, which consists of two modules moduleA and modules.

moduleB MyService.java

package com.company.packageB;
public class MyService {
    public String getHelloWorld(){
        return "Hello world!";
    }
}

module-info.java

module moduleB {}

moduleA module-info.java

module moduleA {}

Main.java

package com.company.packageA;   
import com.company.packageB.MyService;
    public class Main {
        public static void main(String[] args) {
            MyService myService = new MyService();
            String s = myService.getHelloWorld();
            System.out.println(s);
        }
    }

I'd like to run this project. I compile moduleB with this command

javac -d out/moduleB $(find ./moduleB -name "*.java")

and then I'd like to compile moduleA,

javac -p out/moduleB --add-reads moduleA=moduleB --add-exports moduleB/com.company.packageB=moduleA  -d out/moduleA   $(find ./moduleA -name "*.java")

but I run into the issue:

warning: module name in --add-reads option not found: moduleB
warning: [options] module name in --add-exports option not found: moduleB
./moduleA/src/com/company/packageA/Main.java:3: error: package com.company.packageB does not exist
import com.company.packageB.MyService;
                           ^
./moduleA/src/com/company/packageA/Main.java:7: error: cannot find symbol
        MyService myService = new MyService();
        ^
  symbol:   class MyService
  location: class Main

How can I compile moduleA with --add-export and --add-reads options?

Naman
  • 27,789
  • 26
  • 218
  • 353
wakedeer
  • 493
  • 5
  • 14

1 Answers1

1

You should export the package before trying to use the classes from within it in another module.

So, at first the moduleB should export the package as:

module moduleB {
     exports com.company.packageB;
}

and then the other module moduleA should require the moduleB -

module moduleA {
     requires moduleB;
}
Naman
  • 27,789
  • 26
  • 218
  • 353