You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Maven的JavaFX无法导入同项目模块的编译问题

Maven编译报错「cannot find symbol」但IDE可正常访问类的问题

项目目录结构

main-module (Java module)
   sub-module-to-import (Java module)
      ImportedClass.java
   sub-module (Java module)
      fx-sub-module (JavaFX module)
         - pom.xml and classes.

问题描述

已移除所有module-info.java以规避Java模块化特性,使用IntelliJ IDEA开发。在fx-sub-module中通过Maven引入sub-module-to-import后,IDE内可正常实例化ImportedClass、调用其方法,也能通过Ctrl+左键跳转至sub-module-to-import的pom.xml,重新加载Maven项目后无XML错误或警告。但执行mvn clean compile时,出现「cannot find symbol」编译错误,错误指向ImportedClass的方法调用。

fx-sub-module的pom.xml配置

<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 http://maven.apache.org/maven-v4_0_0.xsd">
    <parent>
        <artifactId>sub-module</artifactId>
        <groupId>com.project</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>fx-sub-module</artifactId>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>16</maven.compiler.source>
        <maven.compiler.target>16</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.project</groupId>
            <artifactId>sub-module-to-import</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>13</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>13</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>16</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.6</version>
                <executions>
                    <execution>
                        <id>default-cli</id>
                        <configuration>
                            <mainClass>com.project.sub-module.fx-sub-module.MainClass</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

已尝试的无效操作

  • 将sub-module-to-import添加至fx-sub-module的父pom.xml
  • 修改maven-compiler-plugin、javafx-maven-plugin及依赖版本

可能的解决步骤

1. 验证依赖模块的可构建性

  • 确认sub-module-to-import的pom.xml中groupId、artifactId、version与fx-sub-module依赖配置完全一致
  • 单独进入sub-module-to-import目录执行mvn clean install,确保该模块无编译错误且成功安装到本地Maven仓库(检查~/.m2/repository/com/project/sub-module-to-import/1.0-SNAPSHOT/下是否存在jar包)

2. 升级并完善编译插件配置

旧版本maven-compiler-plugin对Java 16的兼容性可能存在问题,升级至稳定版本并统一编译参数:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.11.0</version>
    <configuration>
        <source>16</source>
        <target>16</target>
        <release>16</release>
        <encoding>UTF-8</encoding>
    </configuration>
</plugin>

3. 完善多模块聚合配置

  • 检查main-module的pom.xml是否包含所有子模块的聚合配置,确保Maven按依赖顺序构建:
    <modules>
        <module>sub-module-to-import</module>
        <module>sub-module</module>
    </modules>
    
  • 从main-module根目录执行mvn clean compile,而非仅在fx-sub-module目录执行,保证依赖模块优先编译

4. 清理缓存

  • 执行mvn clean install -U强制更新快照依赖
  • 在IntelliJ IDEA中执行File → Invalidate Caches...并重启IDE,清理本地缓存

5. 检查类的访问权限

确认ImportedClass及其被调用的方法均为public修饰,Maven编译时不会识别跨模块的包访问权限类/方法(即使IDE允许)

内容的提问来源于stack exchange,提问作者Yoan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 11:45:25