Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error:(32, 13) error: cannot access Referenceable class file for javax.naming.Referenceable not found

Tags:

java

android

i got the error above and below is my code what am i doin wrong? the error reference to the datasource. what is the problem?

public class DBConnector {
private Connection dbConn = null;
private FileLogger fileLogger = new FileLogger("./logs/logfile.txt");
private ConfigurationReader conf = new ConfigurationReader("./config.sys");
private Logger logger = fileLogger.getLogger();

/**
 * Attempts to establish a connection with a test MySQL database.
 */
public DBConnector() {


    MysqlDataSource dataSource = new MysqlDataSource();
    dataSource.setUser(conf.getConfigString("username"));
    dataSource.setPassword(conf.getConfigString("password"));
    dataSource.setServerName(conf.getConfigString("dbhost"));
    dataSource.setDatabaseName(conf.getConfigString("dbname"));
    dataSource.setPort(conf.getConfigInt("dbport"));

    try {
        dbConn = dataSource.getConnection();

        logger.info("Succesful connection with database!");
    } catch (Exception ex) {
        logger.info("Error: DBConnector(): problem creating database connection");
    }
    logger.info("Database connection = " + dbConn);
}
like image 673
Chillings Avatar asked Dec 13 '22 20:12

Chillings


2 Answers

I got the same error (on Java 9 or above). For me the problem was due to a missing requires java.naming; directive in the module-info.java of my module (the module, that accesses Referenceable). Adding this directive fixed it:

module MyModule {
   requires java.naming;
   ...
}

I hope this helps.

like image 187
Rene Knop Avatar answered Feb 23 '23 09:02

Rene Knop


As explained by Sander Mak here this usually happens when your code depends on something that does not have a module descriptor and this dependency's dependency is unresolved (e.g. you're using Hibernate which has no module descriptor but it requires java.naming).

In this case, rather than polluting your own module descriptor with the transitive dependency, Sander suggests to add the transitive dependency to your module graph using

--add-modules java.naming

This way when Hibernate in the future gets its own module descriptor you can just remove the above line and have only your direct dependencies in your module-info.java.

like image 35
machinery Avatar answered Feb 23 '23 11:02

machinery