23

I have created a multimodule project with the following structure

    myproject
      |- mymodule
         |- src
            |- main
               |- java
                  |- com
                     |- mymodule
                        |- Util.java

      |-newmodule
         |-src
           |-main
             |-java
               |-com
                 |-newmodule
                    |- Main.java
             |-module-info.java

Now i want to use Util.java which is a non modularized code in a modularized module newmodule. i have declared following in newmodule

module newmodule {
    requires mymodule;
}

Project is compiling fine, but Intellij is showing module not found and package com.mymodule is declared in unnamed module , module 'newmodule' does not read it.

How to resolve this issue?

And one more question does all the old non modular code is by default turn into automatic-module in java 9 if i don't even modularized legacy modules?

3
  • 1
    by any chance, are these maven sub-modules? and both of them are compiled using IntelliJ? The reason, why I am asking this is, one clear way to resolve this is to make the mymodule as an explicit module with module-info.java in it as well(exports the package as well). Commented Aug 17, 2018 at 4:38
  • 2
    Hi, Actually I don't want to modularize the module named "mymodule" as this is a legacy code. but i want to use Util.java from mymodule in newmodule which is modularized. And yes this is a maven project and i have also declared automatic-module-name for the mymodule in its pom.xml. Sorry i forgot to mention these earlier. Commented Aug 17, 2018 at 6:05
  • 3
    Note: Depending on the JDK version you are using the javac error might incorrectly use the package name as module name as well, e.g. "package mypackage is declared in the unnamed module, but module mypackage does not read it", see JDK-8233524. Commented Oct 24, 2021 at 16:20

1 Answer 1

16

One clear way to resolve this is to make the mymodule as an explicit module as well. This would just be the ideal world of modules I would say.

You can do that by including a module-info.java in mymodule as well, something like -

module mymodule {
    exports com.mymodule;
}

does all the old non modular code is by default turn into automatic-module in java 9 if i don't even modularized legacy modules?

The concept of both the unnamed module and automatic module is to aid the migration and provide the compatibility with the existing classpath techniques.

On one hand, the dependencies of your module which are still not themselves modular and you would still rely on them being one, can be used on the module path for the module system to implicitly define them, when treated as automatic modules and bridge the bottom-up migration expected by JPMS.

The unnamed modules on the other hand relies on the type that is not defined in any module and is resolved to be still found on the classpath. This ensures that every type resolved is part of some module(if nothing then the unnamed module) and also provides the compatibility such that code of existing applications relying on the classpath shall compile and run similarly on module system as well.


The reason, why you would fail to declare an explicit dependence in your code is stated clearly in the doc:-

The unnamed module exports all of its packages. This enables flexible migration, as we shall see below. It does not, however, mean that code in a named module can access types in the unnamed module. A named module cannot, in fact, even declare a dependence upon the unnamed module. This restriction is intentional, since allowing named modules to depend upon the arbitrary content of the class path would make reliable configuration impossible.

Sign up to request clarification or add additional context in comments.

12 Comments

@user10237300 Simplest way that I know of is to create a jar out of your existing code and introduce that as a dependency to your modular code. "How does JPMS know"... is explained in detail in the link shared in the answer. Related to making sure the artifact is used with correct automatic module name refer to this answer from me.
Okay.. so basically if i mention the automatic-module-name in the pom.xml (mymodule) within the maven-jar-plugin and mention the maven dependency in the pom.xml of the new_module which is modularized infact, then the mymodule should be imported as automatic-module in new_module. Is my notion correct? Thank you so much for your help. Because i did the same in my project i mentioned earlier and it compiled fine through cmd, but through intellij it is giving error..
"So basically every old legacy code from unnamed module will be turned into automatic-module if i mentioned that i do manifest entry while creating the jar file"...only when that jar file is available at the modulepath of your application relying on it.
"mention the maven dependency in the pom.xml of the new_module which is modularized infact, then the mymodule should be imported as automatic-module in new_module. Is my notion correct? "...correct unless you've explicitly made that mymodule as a modular application itself.
Yes my doubt is clear now. Thanks a lot. But yeah facing some different issue.will open a diff thread.
|

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.