IntelliJ运行时提示‘java: module not found: javafx.web’问题求助
问题:运行JavaFX项目时提示
java: module not found: javafx.web 我在项目中尝试使用WebView组件,已在pom.xml中引入对应依赖、在module-info.java中声明javafx.web模块,IntelliJ编辑代码时未报错,能正常识别该库并查询类及其方法,但运行程序时却出现java: module not found: javafx.web的错误。
相关代码
module-info.java
module com.gabrielgavrilov.cobolt { requires javafx.controls; requires javafx.fxml; requires javafx.web; requires org.fxmisc.richtext; opens com.gabrielgavrilov.cobolt.core to javafx.fxml; exports com.gabrielgavrilov.cobolt.core; opens com.gabrielgavrilov.cobolt.client to javafx.fxml; exports com.gabrielgavrilov.cobolt.client; }
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.gabrielgavrilov</groupId> <artifactId>cobolt</artifactId> <version>1.0-SNAPSHOT</version> <name>Cobolt</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.version>5.9.2</junit.version> </properties> <dependencies> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>17.0.6</version> </dependency> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-fxml</artifactId> <version>17.0.6</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.fxmisc.richtext/richtextfx --> <dependency> <groupId>org.fxmisc.richtext</groupId> <artifactId>richtextfx</artifactId> <version>0.11.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-web --> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-web</artifactId> <version>20</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>11</source> <target>11</target> </configuration> </plugin> <plugin> <groupId>org.openjfx</groupId> <artifactId>javafx-maven-plugin</artifactId> <version>0.0.8</version> <executions> <execution> <!-- Default configuration for running with: mvn clean javafx:run --> <id>default-cli</id> <configuration> <mainClass>com.gabrielgavrilov.cobolt/com.gabrielgavrilov.cobolt.client.Cobolt</mainClass> <launcher>app</launcher> <jlinkZipName>app</jlinkZipName> <jlinkImageName>app</jlinkImageName> <noManPages>true</noManPages> <stripDebug>true</stripDebug> <noHeaderFiles>true</noHeaderFiles> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
解决方案
1. 统一JavaFX依赖版本
pom.xml中javafx-controls、javafx-fxml使用17.0.6版本,但javafx-web用的是20版本,版本不匹配会引发模块加载异常。将javafx-web的版本修改为和其他JavaFX依赖一致的17.0.6:
<dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-web</artifactId> <version>17.0.6</version> </dependency>
2. 配置javafx-maven-plugin模块参数
在javafx-maven-plugin的configuration节点中添加<modules>标签,明确指定运行时需要加载的JavaFX模块:
<configuration> <mainClass>com.gabrielgavrilov.cobolt/com.gabrielgavrilov.cobolt.client.Cobolt</mainClass> <modules> <module>javafx.controls</module> <module>javafx.fxml</module> <module>javafx.web</module> </modules> <launcher>app</launcher> <jlinkZipName>app</jlinkZipName> <jlinkImageName>app</jlinkImageName> <noManPages>true</noManPages> <stripDebug>true</stripDebug> <noHeaderFiles>true</noHeaderFiles> </configuration>
3. 清理并重新构建项目
执行以下Maven命令清理旧构建文件,重新编译打包:
mvn clean install
之后使用mvn javafx:run启动项目即可解决模块找不到的问题。
内容的提问来源于stack exchange,提问作者Gabriel Gavrilov
相关产品推荐
相关产品推荐

