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

我创建了两个模块化程序。一个是 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-info.java如下内容的 Maven 项目:


module TextInputProgram {

    requires selenium.api;

    requires selenium.chrome.driver;

}

因此,我将模块化 Maven 项目作为外部 jar 添加到 JavaFX 中(通过 libs 中的项目结构和通过我指定的 jar 在模块信息中requires)。我还添加了包含用于编译的外部 jar 的变量(除了PATH_TO_FX和PATH_TO_FX_MODS):


set EXTERNAL_JAR="...\out\artifacts\TextInputProgram_jar"

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


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我在 JavaFX 项目中导入以使用来自外部 jar 的类。


更新:


如果我exports project;在 maven 项目的 module-info.java 中添加,那么我会在 JavaFX module-info.java 中看到错误:

http://img2.mukewang.com/646dcb0b0001a49f06580168.jpg

如果我删除,requires TextInputProgram;那么导入时会出现另一个错误

http://img4.mukewang.com/646dcb17000146b806440074.jpg

我在这里错过了什么吗?我正在尝试获取这两个程序的可执行 Jar。



慕田峪9158850
浏览 162回答 1
1回答

慕仙森

正如评论中所讨论的那样,这里可能存在一些问题:模块定义本地 jar 和依赖项基于发布的模块化项目,我将创建一个小型演示多模块化项目来解释这两个问题。Maven 多模块项目使用您的 IDE,创建一个 Maven 项目,然后添加两个模块,TextInputProgram例如checker:父pom<groupId>org.openjfx</groupId><artifactId>hellofx</artifactId><version>1.0-SNAPSHOT</version><modules>&nbsp; &nbsp; <module>TextInputProgram</module>&nbsp; &nbsp; <module>checker</module></modules><packaging>pom</packaging>TextInputProgram pom<parent>&nbsp; &nbsp; <groupId>org.openjfx</groupId>&nbsp; &nbsp; <artifactId>hellofx</artifactId>&nbsp; &nbsp; <version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>TextInputProgram</artifactId><properties>&nbsp; &nbsp; <maven.compiler.source>11</maven.compiler.source>&nbsp; &nbsp; <maven.compiler.target>11</maven.compiler.target>&nbsp; &nbsp; <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies>&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.seleniumhq.selenium</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>selenium-java</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>3.141.59</version>&nbsp; &nbsp; </dependency></dependencies><build>&nbsp; &nbsp; <plugins>&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-compiler-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.8.1</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <release>11</release>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; </plugins></build>Module-info.javamodule TextInputProgram {&nbsp; &nbsp; requires selenium.api;&nbsp; &nbsp; requires selenium.chrome.driver;&nbsp; &nbsp; exports project to checker;}检查 pom<parent>&nbsp; &nbsp; <artifactId>testMods</artifactId>&nbsp; &nbsp; <groupId>org.openjfx</groupId>&nbsp; &nbsp; <version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>checker</artifactId><dependencies>&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.openjfx</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>javafx-controls</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>12.0.2</version>&nbsp; &nbsp; </dependency>&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.openjfx</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>javafx-fxml</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>12.0.2</version>&nbsp; &nbsp; </dependency>&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.openjfx</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>TextInputProgram</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>1.0-SNAPSHOT</version>&nbsp; &nbsp; </dependency></dependencies><build>&nbsp; &nbsp; <plugins>&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-compiler-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.8.1</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <release>11</release>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.openjfx</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>javafx-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>0.0.3</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <mainClass>sample.Main</mainClass>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; </plugins></build>Module-info.javamodule checker {&nbsp; &nbsp; requires javafx.fxml;&nbsp; &nbsp; requires javafx.controls;&nbsp; &nbsp; requires TextInputProgram;&nbsp; &nbsp; opens sample to javafx.fxml;&nbsp; &nbsp; exports sample;}(此时两个模块的代码无关紧要)。包括解决方案现在请注意,您必须将其添加到TextInputProgram模块描述符中:exports project to checker;如果你想让checker模块访问的TextInputProgram包project。这将解决此错误:包项目在模块 TextInputProgram 中声明,不导出它另请注意,我已将此依赖项包含在checker项目中:<dependency>&nbsp; &nbsp; <groupId>org.openjfx</groupId>&nbsp; &nbsp; <artifactId>TextInputProgram</artifactId>&nbsp; &nbsp; <version>1.0-SNAPSHOT</version></dependency>即使有两个模块,在同一个父模块下,您也需要包含一个模块到另一个模块的依赖关系。否则这将失败(找不到模块):requires TextInputProgram;mvn clean install您可以在模块中运行TextInputProgram,以安装org.openjfx:TextInputProgram:1.0-SNAPSHOT在您的本地存储库中.m2。然后您可以运行mvn clean javafx:runintchecker module来运行 GUI 项目。值得一提的是,将两个 maven 依赖项与 jar 文件结合起来并不是一个好主意。如果您最终同时使用两者,您将收到此错误:模块“检查器”从“TextInputProgram”和“TextInputProgram”中读取“项目”此消息意味着模块路径中有两个模块具有完全相同的名称,一个是 maven 依赖项,另一个是 jar 工件。为了分发您的项目和模块,最好使用 maven 方法,并避免在 lib 文件夹中包含 jar 文件。稍后你的模块将作为单独的工件发布,你不需要修改你的 pom 文件(除了更新版本号)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java