Java 9错误:不在模块源路径上的模块中 [英] Java 9 error: not in a module on the module source path

查看:191
本文介绍了Java 9错误:不在模块源路径上的模块中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目结构



我有一个用Java 8编写的项目,我想将其更新为Java9。因此,我将类分为2个独立的模块。模块:




  • org.ggp.base 模块目录 org.ggp.base / src / main / java 中的-info.java 。在我开始对Java 9进行更新之前,它的构建是通过Gradle实现的。该模块使用 pl.edu.prz.klopusz 模块中包含的抽象类实现。
  • 目录 pl.edu.prz.klopusz / dolar-app / src /中的
  • pl.edu.prz.klopusz main / java 。我想使用Maven自动化其构建。该模块需要 org.ggp.base 模块。



文件树看起来例如:

 
├──org.ggp.base /
│├──build.gradle
│└──src /
│└──main /
│├ ──java /
││├──外部/
││││└──JSON /
│││││├──JSONArray.java
││││└── JSONObject.java
││├──META-INF /
││││└──MANIFEST.MF
││├──module-info.java
│││└ ──org /
││└──ggp /
│││└──base /
││└──util /
││├──Statemachine /
│││├──MachineState.java
│││││└──StateMachine.java
││└──符号/
│└──资源/
│ └──org /
│└──ggp /
│└──base /
└──pl.edu.prz.klopusz /
└──dolar-app /
└──src /
└──main /
└──java /
├──module-info.java
└──pl /
└──edu /
└─ ──prz /
└──klopusz /
└──Utility /
└──装饰器
└──StateMachineDecorator.java

module-info.java 文件的内容如下:



org.ggp.base / src / main / java / module-info.java

 模块org.ggp.base {
需要番石榴;
需要反思;
需要jdk.httpserver;

使用org.ggp.base.util.statemachine.StateMachine;

出口org.ggp.base;
}



pl.edu.prz.klopusz / dolar-app / src / main / java / module-info.java

  module pl.edu.prz.klopusz {
需要org.ggp.base ;

提供org.ggp.base.util.statemachine.StateMachine
和pl.edu.prz.klopusz.utilities.decorators.StateMachineDecorator;
}



编译



我尝试使用以下命令编译项目:

  javac -d out \ 
--module-源路径org.ggp.base / src / main / java:pl.edu.prz.klopusz / dolar-app / src / main / java \
$(查找org.ggp.base / src / main / java -name * .java)\
$(找到pl.edu.prz.klopusz / dolar-app / src / main / java -name * .java)



错误



我收到以下错误:


org.ggp.base / src / main / java / module-info.java:1:

错误:在模块源路径上找不到模块


模块org.ggp.base {


<$ c中的类还有其他99个错误$ c> org.ggp.base 软件包,每个软件包都像这样:


org.ggp.base / src /main/java/external/JSON/JSONObject.java:1:

错误:不在模块源路径上的模块中 
package external.JSON;





org.ggp.base / src / main / java / org / ggp / base / validator / OPNFValidator.java:1:

错误:不在模块源路径上的模块中 
包org.ggp.base.validator;




我想要的东西



我想摆脱错误并进行编译该项目。我不必保留目录结构,但是当我将它们全部组合在一起时,使用2个 module-info.java 文件,编译器会抱怨多个模块。我可以通过IntelliJ IDEA完成它,我不介意,我尝试了。但是我也不知道幕后发生了什么,也不知道如何处理错误(包为空:org.ggp.base )。





已经找到



很难在<$ c中找到有关-module-source-path 开关的文档$ c> javac 命令。 我找到了这个网站。它说:

如果您将代码放置在
模块中,从而将模块的代码放在以
命名的封闭目录中模块,模块源路径变得更像
a简单路径,如

-module-source-path Users / Me / MyProject / src 


或如果它在多个项目中,请使用

 
--module-source-path
/ Users / Me / MyProject / src:/ Users / Me / MyOtherProject / src


对于Windows,请使用反斜杠和分号,但无论如何我都在使用Linux。



对于问题 ,与我的错误相同,但我想它仍然没有解决。



UPDATE



-verbose 开关



我在命令末尾添加了 -verbose 开关。这就是编译器说的一部分:

  [解析开始的SimpleFileObject [/home/sensitive/org.ggp.base/src /main/java/module-info.java]] 
[解析开始的SimpleFileObject [/home/sensitive/org.ggp.base/src/main/java/org/ggp/base/util/statemachine/MachineState。 java]]
[解析开始的SimpleFileObject [/home/sensitive/pl.edu.prz.klopusz/dolar-app/src/main/java/module-info.java]]
[解析开始的SimpleFileObject [/home/sensitive/pl.edu.prz.klopusz/dolar-app/src/main/java/pl/edu/prz/klopusz/utilities/decorators/StateMachineDecorator.java]]
org.ggp.base /src/main/java/org/ggp/base/util/statemachine/MachineState.java:1:错误:不在模块源路径
包中的模块中org.ggp.base.util.statemachine;
^
[加载/modules/jdk.httpserver/module-info.class]
[加载/modules/java.base/module-info.class]
[总计263ms ]
100错误

所以,我认为这不是 --module-source-path 开关(@StephanHerrmann指出的 * src / main / java 也会发生相同的行为)。它读取应读取的所有Java源。 pl.edu.prz.klopusz 模块中的源没有问题。
这些是 org.ggp.base.util.statemachine.MachineState 的第一行:

 包org.ggp.base.util.statemachine; 

import org.ggp.base.util.gdl.grammar.GdlSentence;

import java.util.HashSet;
import java.util.Set;

公共类MachineState {
// ...
}


解决方案

根据 JEP 261 code>-module-source-path 选项(用于多模块模式下的编译)必须指向一个目录,该目录为每个包含的模块保存一个子目录,该目录名必须等于模块名称。



要适应源代码未直接包含在模块目录中的布局,该选项支持 patterns ,其中标记 * 可以用来表示路径的任何部分中的模块名称,例如 ./*/ src / main / java / ,它将在中找到模块 my.mod1 。/my.mod1 / src / main / java / module-info.java 等。



JEP 261在模式 * 中未提及任何矛盾之处可能会发生,但是显然 javac 不喜欢开始模式与 *



有点关系,我可能会补充说,在先前的讨论中,我被告知JEP 261包含过时的信息,但是我的问题是,是否以及在何处该规范将在JEP完成后得到维护,但没有答案。 javac手动输入并不是为诸如以下选项提供足够详细信息的地方-模块源路径


Project structure

I have a project written in Java 8 and I want to update it to Java 9. So I separated the classes into 2 separate modules. Modules:

  • org.ggp.base with module-info.java in the directory org.ggp.base/src/main/java. Its build was automated with Gradle before I started the update to Java 9. The module uses an abstract class implementation included in pl.edu.prz.klopusz module.
  • pl.edu.prz.klopusz in the directory pl.edu.prz.klopusz/dolar-app/src/main/java. I want to automate its build using Maven. The module requires org.ggp.base module.

The file tree looks like:

.
├── org.ggp.base/
│   ├── build.gradle
│   └── src/
│       └── main/
│           ├── java/
│           │   ├── external/
│           │   │   └── JSON/
│           │   │       ├── JSONArray.java
│           │   │       └── JSONObject.java
│           │   ├── META-INF/
│           │   │   └── MANIFEST.MF
│           │   ├── module-info.java
│           │   └── org/
│           │       └── ggp/
│           │           └── base/
│           │               └── util/
│           │                   ├── statemachine/
│           │                   │   ├── MachineState.java
│           │                   │   └── StateMachine.java
│           │                   └── symbol/
│           └── resources/
│               └── org/
│                   └── ggp/
│                       └── base/
└── pl.edu.prz.klopusz/
    └── dolar-app/
        └── src/
            └── main/
                └── java/
                    ├── module-info.java
                    └── pl/
                        └── edu/
                            └── prz/
                                └── klopusz/
                                    └── utilities/
                                        └── decorators
                                          └──StateMachineDecorator.java

The contents of module-info.java files is the following:

org.ggp.base/src/main/java/module-info.java

module org.ggp.base {
    requires guava;
    requires reflections;
    requires jdk.httpserver;

    uses org.ggp.base.util.statemachine.StateMachine;

    exports org.ggp.base;
}

pl.edu.prz.klopusz/dolar-app/src/main/java/module-info.java

module pl.edu.prz.klopusz {
    requires org.ggp.base;

    provides org.ggp.base.util.statemachine.StateMachine
        with pl.edu.prz.klopusz.utilities.decorators.StateMachineDecorator;
}

Compilation

I try to compile the project, using the following command:

javac -d out \
  --module-source-path org.ggp.base/src/main/java:pl.edu.prz.klopusz/dolar-app/src/main/java \
  $(find org.ggp.base/src/main/java -name *.java) \
  $(find pl.edu.prz.klopusz/dolar-app/src/main/java -name *.java)

Errors

I get the following error:

org.ggp.base/src/main/java/module-info.java:1:

error: module not found on module source path

module org.ggp.base {

And 99 other errors for the classes inside org.ggp.base package, each is like:

org.ggp.base/src/main/java/external/JSON/JSONObject.java:1:

error: not in a module on the module source path

package external.JSON;

or

org.ggp.base/src/main/java/org/ggp/base/validator/OPNFValidator.java:1:

error: not in a module on the module source path

package org.ggp.base.validator;

What I want

I want to get rid of the errors and compile the project. I don't have to preserve the directory structure, but when I put it all together, with 2 module-info.java files, the compiler complainted about multiple modules. I can have it done by IntelliJ IDEA, I don't mind and I tried. But I don't know what is happening behind the scenes, and I don't know how to handle the errors either (Package is empty: org.ggp.base).

What I've already found

It was hard to find a documentation about --module-source-path switch in javac command. This site is what I found. It says:

if you arrange the code in your modules such that you put the code for a module in an enclosing directory named for the module, the module source path becomes more like a simple path, as in

--module-source-path Users/Me/MyProject/src

or if it is in multiple projects, use

    --module-source-path 
        /Users/Me/MyProject/src:/Users/Me/MyOtherProject/src

And for Windows use backslashes and semicolons, but I'm using Linux anyway.

There is also a comment to an issue on OpenJDK bugs site, with the same error as mine, but I guess it remains unsolved.

UPDATE

-verbose switch

I added -verbose switch at the end of command. That is a part of what compiler says:

[parsing started SimpleFileObject[/home/sensitive/org.ggp.base/src/main/java/module-info.java]]
[parsing started SimpleFileObject[/home/sensitive/org.ggp.base/src/main/java/org/ggp/base/util/statemachine/MachineState.java]]
[parsing started SimpleFileObject[/home/sensitive/pl.edu.prz.klopusz/dolar-app/src/main/java/module-info.java]]
[parsing started SimpleFileObject[/home/sensitive/pl.edu.prz.klopusz/dolar-app/src/main/java/pl/edu/prz/klopusz/utilities/decorators/StateMachineDecorator.java]]
org.ggp.base/src/main/java/org/ggp/base/util/statemachine/MachineState.java:1: error: not in a module on the module source path
package org.ggp.base.util.statemachine;
^
[loading /modules/jdk.httpserver/module-info.class]
[loading /modules/java.base/module-info.class]
[total 263ms]
100 errors

So, I think this is not a fault of the path in --module-source-path switch (the same behavior happens with *src/main/java pointed out by @StephanHerrmann). It reads all the java sources it should read. There's no problem with sources in the pl.edu.prz.klopusz module. These are the first lines of org.ggp.base.util.statemachine.MachineState:

package org.ggp.base.util.statemachine;

import org.ggp.base.util.gdl.grammar.GdlSentence;

import java.util.HashSet;
import java.util.Set;

public class MachineState {
    //...
}

解决方案

As per JEP 261 the --module-source-path option (for compilation in "multi-module mode") must point to a directory that holds one subdirectory for each contained module, where the directory name must equal the module name.

To accommodate layouts where sources are not directly contained in the module directory, the option supports patterns where the token * can be used to represent the module name in any part of the path such as in "./*/src/main/java/", which will find the module my.mod1 in ./my.mod1/src/main/java/module-info.java etc.

JEP 261 does not mention any contraints on where in the pattern * may occur, but apparently javac doesn't like patterns starting with *. This may or may not be intentional.

Slightly related, I might add that in a previous discussion I was informed that JEP 261 contains outdated information, but my question whether and where this specification would be maintained after the JEP was completed, produced no answer. The javac manual entry is not the place that gives sufficient details for options like --module-source-path.

这篇关于Java 9错误:不在模块源路径上的模块中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆