Java 11包在不导出它的模块中声明

Java 11包在不导出它的模块中声明,java,javafx,Java,Javafx,我创建了两个模块化程序。一个是JavaFX模块化项目,它包含module info.java类似: module checker { requires javafx.fxml; requires javafx.controls; requires TextInputProgram; // Btw, IDEA shows me here "Ambiguous module reference" exports sample; opens sample; }

我创建了两个模块化程序。一个是JavaFX模块化项目,它包含
module info.java
类似:

module checker {
    requires javafx.fxml;
    requires javafx.controls;
    requires TextInputProgram; // Btw, IDEA shows me here "Ambiguous module reference"
    exports sample;
    opens sample;
}
module TextInputProgram {
    requires selenium.api;
    requires selenium.chrome.driver;
}
另一个包含
module info.java
的Maven项目,如:

module checker {
    requires javafx.fxml;
    requires javafx.controls;
    requires TextInputProgram; // Btw, IDEA shows me here "Ambiguous module reference"
    exports sample;
    opens sample;
}
module TextInputProgram {
    requires selenium.api;
    requires selenium.chrome.driver;
}
因此,我将模块化maven项目作为外部jar添加到JavaFX中(通过libs中的项目结构和通过
requires
I指定的jar在模块信息中)。此外,我还添加了包含用于编译的外部jar的变量(除了
PATH\u to_FX
PATH\u to_FX\u MODS
):

但当我试图通过命令编译项目时:

dir /s /b src\*.java > sources.txt & javac --module-path %EXTERNAL_JAR%;%PATH_TO_FX% -d mods/checker @sources.txt & del sources.txt
跟随

我从JavaFX项目类中得到错误:

\src\sample\Controller.java:9: error: package project is not visible
import project.SeleniumProgram;
       ^
  (package project is declared in module TextInputProgram, which does not export it)
1 error

Package is declared in module which does not export it
import project.SeleniumProgram
I在JavaFX项目中导入,以使用外部jar中的类

更新:

如果我添加
导出项目maven项目的module-info.java内部然后我在JavaFX module-info.java中看到错误:

如果我删除
需要TextInputProgram然后导入时出现另一个错误:


我是不是遗漏了什么?我正在尝试获取这两个程序的可执行Jar。

正如在整个评论中所讨论的,这里有一些可能的问题:

  • 模块定义

  • 本地jar和依赖项

基于发布的模块化项目,我将创建一个小型演示多模块项目来解释这两个问题

Maven多模块项目 使用IDE创建一个Maven项目,然后添加两个模块,
TextInputProgram
checker
,如:

父pom

<groupId>org.openjfx</groupId>
<artifactId>hellofx</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
    <module>TextInputProgram</module>
    <module>checker</module>
</modules>
<packaging>pom</packaging>
<parent>
    <groupId>org.openjfx</groupId>
    <artifactId>hellofx</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>TextInputProgram</artifactId>

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <release>11</release>
            </configuration>
        </plugin>
    </plugins>
</build>
<parent>
    <artifactId>testMods</artifactId>
    <groupId>org.openjfx</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>checker</artifactId>

<dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>12.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>12.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>TextInputProgram</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <release>11</release>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.3</version>
            <configuration>
                <mainClass>sample.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>
棋盘格pom

<groupId>org.openjfx</groupId>
<artifactId>hellofx</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
    <module>TextInputProgram</module>
    <module>checker</module>
</modules>
<packaging>pom</packaging>
<parent>
    <groupId>org.openjfx</groupId>
    <artifactId>hellofx</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>TextInputProgram</artifactId>

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <release>11</release>
            </configuration>
        </plugin>
    </plugins>
</build>
<parent>
    <artifactId>testMods</artifactId>
    <groupId>org.openjfx</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>checker</artifactId>

<dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>12.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>12.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>TextInputProgram</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <release>11</release>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.3</version>
            <configuration>
                <mainClass>sample.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>
(此时,两个模块的代码无关)

解决方案包括 现在请注意,您必须将其添加到
TextInputProgram
模块描述符中:

exports project to checker;
如果您想让
检查器
模块访问
文本输入程序
项目

这将解决此错误:

包项目在模块TextInputProgram中声明,模块TextInputProgram不导出它

另外,请注意,我已将此依赖项包括在
检查器
项目中:

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>TextInputProgram</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
您可以在
TextInputProgram
模块中运行
mvn clean install
,在本地存储库
.m2
中安装
org.openjfx:TextInputProgram:1.0-SNAPSHOT

然后您可以运行
mvncleanjavafx:run
int
checker模块
,以运行GUI项目

值得一提的是,将maven依赖项和jar文件结合起来不是一个好主意。如果最终同时使用这两个选项,则会出现以下错误:

模块“checker”从“TextInputProgram”和“TextInputProgram”读取“project”

这个消息意味着模块路径中有两个模块具有完全相同的名称,一个是maven依赖项,另一个是jar工件


为了分发项目和模块,最好使用maven方法,并避免在lib文件夹中包含jar文件。稍后,您的模块将作为单独的工件发布,您不需要修改pom文件(除了更新版本号)。

如果模块的所有公共包都没有打开或导出,那么将
TextInputProgram
模块添加到JavaFX项目是不够的。您需要添加类似于
导出项目
的内容,以便任何模块都可以访问此软件包,或者如果您只想访问给定模块,请添加
将项目导出到checker
。谢谢您的回答,JoséPereda。是的,尝试在maven项目内的模块信息中使用命令
exports project
,但是当我打开使用此外部jar的JavaFx项目模块信息时,我看到错误:模块从“TextInputProgram”和“TextInputProgram”读取包。当我删除
时需要TextInputProgram
那么我看不到问题,但我需要它,因为导入需要它。如果您使用
打开project to checker
而不是
exports
,它是否修复了错误?看起来您有两个不同版本的相同TextInputProgram jar?您可以在外部库中验证这一点。澄清一下:我有两个不同的项目。如果我使用
将项目打开到checker在maven项目中,IDEA告诉我没有找到模块检查器(通过黄色标记强调我)。在JavaFX模块中,我仍然有:
模组引用不明确
。在外部libs中,我只有一个版本TextInputProgram。我需要合并两个项目吗?作为一个有多个模块的maven项目可能会更好?