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

VS Code+JDK19+Maven环境下javax.sql.rowset包无法访问问题

解决javax.sql.rowset包无法访问的问题

问题背景

正在实现CachedRowSet,开发环境为VS Code、JDK19和Maven。编写DBUtil.java时,导入javax.sql.rowset包触发报错:The package javax.sql.rowset is not accessible。

相关代码

DBUtil.java:

package searcher;

import javax.sql.rowset;
import java.sql.*;

配置信息

  1. VS Code的settings.json:
{
  "java.configuration.updateBuildConfiguration": "interactive",
  "java.jdt.ls.java.home": "C:\\Program Files\\Java\\jdk-19\\"
}
  1. 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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>searcher</groupId>
    <artifactId>searcher</artifactId>
    <version>1</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <version>8.0.31</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>11</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>4</version>
                <executions>
                    <execution>
                        <!-- Default configuration for running -->
                        <!-- Usage: mvn clean javafx:run -->
                        <id>default-cli</id>
                        <configuration>
                            <mainClass>searcher.App</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

已尝试的无效操作

  • 清理工作区
  • 修改pom.xml配置
  • 在module-info.java中添加requires transitive java.sql.rowset

困惑点

VS Code左侧资源管理器的JRE系统库下,java.sql.rowset模块中明明存在javax.sql.RowSet相关类,但仍报错。


解决方案

1. 修正导入语句

原导入写法错误,Java中导入包需指定具体类或使用通配符:

// 导入单个类
import javax.sql.rowset.CachedRowSet;
import java.sql.*;

// 或导入整个包下的类
import javax.sql.rowset.*;
import java.sql.*;

2. 统一JDK版本配置

pom.xml中编译版本指定为JDK11,但实际使用JDK19,版本不一致会引发模块访问问题。修改pom.xml配置:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>19</maven.compiler.source>
    <maven.compiler.target>19</maven.compiler.target>
</properties>
...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <release>19</release>
    </configuration>
</plugin>

3. 修正module-info.java依赖声明

模块化项目中,确保module-info.java正确声明依赖:

module searcher {
    requires java.sql;
    requires java.sql.rowset;
    requires com.mysql.cj.jdbc;
    requires javafx.controls;
    requires javafx.fxml;
    exports searcher;
}

无需添加transitive,除非需要将java.sql.rowset暴露给依赖当前模块的其他模块。

4. 刷新Maven并重启VS Code

  • 打开VS Code命令面板(Ctrl+Shift+P),执行Maven: Reload Project
  • 重启VS Code让配置完全生效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 06:47:41