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?

3
  • I think you only need to tag your question with the actual java version you are using. Are you really using versions 9, 10 and 11 ? Commented Apr 29, 2020 at 12:56
  • 1
    It should work with version java >=9 Commented Apr 29, 2020 at 15:16
  • If to specify export MAVEN_OPTS="--add-reads demo=ALL-UNNAMED" then WARNING: Unknown module: specified to --add-reads. Commented Nov 27, 2024 at 15:14

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.