Search code examples
javaeclipsejava-9

Java 9 - Cannot be resolved as a module


Very simple use case, I am using Eclipse Oxygen 4.7.3a that includes support from Java 9. I have two projects that are Java 9 projects:

- projectOne
| - src
| - module-info.java
  | - com
    | - package1
      | - first
        | Classificator.java

- projectTwo
| - src
| - module-info.java
  | - com
    | - package2
      | - second
        | Classifiable.java

I want to use the Classifiable class inside the Classificator, so I try to import the second module into the first project.

module-info.java Project 1:

module projectOne   {
    requires projectTwo;
}

module-info.java Project 2:

module projectTwo   {

}

Eclipse is giving me the error:

projectTwo cannot be resolved to a module

Do I have to gather all my Java projects under one "main project" in order to let Eclipse know that all those modules are usable inside it? Or have I missed something else?


Solution

  • No, you don't need them to be in the same directory. Ideally, your project-one module defines some uses, which are implements by your project-two module (or vice-versa). Get the runtime implementation of your used interfaces. For this, both jars/classes must be on the module path.

    For further information on module build, multi module builds,... you can refer to this link. Even if you do not use gradle, its tutorial on java 9 module build is quite interesting and gives some insight.