"The package java.sql is not accessible." in eclipse [duplicate]
Asked Answered
N

1

6
package test;

import java.sql.*;

public class Test2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}

in the above code. import java.sql.*; contain error that is "The package java.sql is not accessible". Please help me to resolve this error. why java.sql is not accessible.

Nuzzi answered 13/12, 2020 at 8:15 Comment(2)
What is the version of JDK configuredHighmuckamuck
13.0.2 version of jdkNuzzi
H
12

I can see the module-info.java file in your screenshot, which means you use Java 9 and above. Please add the following in the module-info.java if you want to use modules for your application

requires java.sql;

If you do not want to use modules, delete the module-info.java

Highmuckamuck answered 13/12, 2020 at 8:22 Comment(4)
From the screenshot you can see that Java 9 or higher is used, otherwise there would be errors shown for the module-info.java file. Deleting the module-info.java file is also a solution for this issue when using Java 9 or higher since it's not mandatory to use JPMS.Gideon
This approach would make main code in /src/main/java unneccessarily depending on java.sql. In 2024, is there a better approach to resolve this Eclipse error (except making the project non-modular by deleting module-info.java)?Monika
@user27772, the original question is "How to use java.sql.* classes in a modular project". If you do not have a dependency on java.sql.*, then remove the requires java.sql; from module-info.java.Highmuckamuck
@seenukarthi, you are right. I was misinterpreting the screenshot and thought that the original question is about a Maven project with test folder but it actually isn't. The problem I had is that as soon as test code (e. g. /src/test/java/Test123.java) in a modular Maven project makes use of java.sql.*, Eclipse complains with "package java.sql is not accessible.". Adding "requires java.sql" to module-info.java will resolve this but I don't want to do this since main code does not require java.sql. Any ideas?Monika

© 2022 - 2024 — McMap. All rights reserved.