Quick Fix: Delete the module-info.java
file from your project. This resolved the error.
The Problem:
Why am I getting a The package com.microsoft.sqlserver.jdbc is not accessible
error when trying to import the Microsoft SQL Server JDBC package in my Java project? I have already added the MSSQL JDBC driver to my classpath, but the import statement still throws an error. Please provide a solution to resolve this issue.
The Solutions:
Solution 1: Remove `module-info.java`
If you see the error `The package com.microsoft.sqlserver.jdbc is not accessible` when importing the JDBC package, you may need to delete the `module-info.java` file. This error is a common problem when using JDK 13. The `module-info.java` file is used to define a modular application with Java 9 or higher. The error occurs because the JDBC driver doesn’t provide a `module-info.java` file. Therefore, we need to remove it from the classpath to resolve the error.
Solution 2: Add `requires java.sql;` to `module-info.java`
Create a file named module-info.java
in the root directory of your Java project. Add the following line:
requires java.sql;
This will allow your project to access the SQL Server JDBC package.
Solution 3: Check Your Project Structure
Instead of creating a regular Java class with a plain .java file, consider the following steps:
- In your project, right-click on the package where you want to create your class.
- Select New > Class.
- Specify a file name for your class and ensure that the parent class is set to Object.
- Click Finish.
By following these steps, you can create a class that is properly recognized by the Java compiler and avoid the “package not accessible” error.