自动模块 未命名模块 java,Java 9迁移问题-包com.mymodule在未命名的模块中声明,模块'newmodule'不读取它...

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?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值