Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.ClassNotFoundException: com.mchange.v2.c3p0.ComboPooledDataSource in IntelliJ while it works fine in Eclipse

I made a simple Java EE app, and I have a problem with connection to database. In eclipse everything works fine, but when I try the same in Intellij errors occur.

package db;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

public class DbUtil {
private static DbUtil dbUtil;
private ComboPooledDataSource connectionPool;

private DbUtil() throws PropertyVetoException {
    connectionPool = new ComboPooledDataSource();
    connectionPool.setDriverClass("com.mysql.jdbc.Driver");
    connectionPool.setJdbcUrl("jdbc:mysql://localhost:3306/world");
    connectionPool.setUser("root");
    connectionPool.setPassword("root");

    connectionPool.setInitialPoolSize(5);
    connectionPool.setMinPoolSize(5);
    connectionPool.setMaxPoolSize(20);
    connectionPool.setAcquireIncrement(5);
    connectionPool.setMaxIdleTime(3600);
}

public Connection getConnection() throws SQLException {
    return connectionPool.getConnection();
}

public void close() {
    connectionPool.close();
}

public static DbUtil getInstance() {
    if (dbUtil == null) {
        try {
            dbUtil = new DbUtil();
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }
    return dbUtil;
}
  }

In project structure - > libraries I have: image

And the errors are:

java.lang.NoClassDefFoundError: com/mchange/v2/c3p0/ComboPooledDataSource
java.lang.ClassNotFoundException: com.mchange.v2.c3p0.ComboPooledDataSource
like image 495
Bartosz Czyżowski Avatar asked Mar 12 '23 21:03

Bartosz Czyżowski


1 Answers

It's been a long time, but I faced the same problem and this solution worked.

In project_name/web/WEB-INF/ create new folder named lib, copy .jars c3p0-0.9.5.2, c3p0-oracle-thin-extras-0.9.5.2, mchange-commons-java-0.2.11 then in Project View select lib folder and finally Add as liblary....

like image 80
BednarQ Avatar answered Mar 19 '23 03:03

BednarQ