In my Java Application I need to use a connection pool, so I decided to implement HibernateCP. The problem is that I get this exception that I need to add this: org/slf4j/LoggerFactory. The full Exception is:
INFO: HHH000490: Using JTA platform [org.hibernate.engine.transaction.jta.platform.internal.JBossStandAloneJtaPlatform]
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.zaxxer.hikari.HikariConfig.<clinit>(HikariConfig.java:48)
at Database_Managment.DatabaseConfig.<clinit>(DatabaseConfig.java:16)
at Database_Managment.DBFunctionality.checkUser(DBFunctionality.java:45)
at DTOS.LogInView4ドル.mouseClicked(LogInView.java:221)
at java.desktop/java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:278)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6629)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
at java.desktop/java.awt.Component.processEvent(Component.java:6391)
at java.desktop/java.awt.Container.processEvent(Container.java:2266)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4584)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
at java.desktop/java.awt.EventQueue4ドル.run(EventQueue.java:722)
at java.desktop/java.awt.EventQueue4ドル.run(EventQueue.java:716)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
at java.desktop/java.awt.EventQueue5ドル.run(EventQueue.java:746)
at java.desktop/java.awt.EventQueue5ドル.run(EventQueue.java:744)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 35 more
I have tried a lot of ways and different specifications and so on, but I didn't learn about Maven or dependencies yet so I have basic knowledge and I might have missed something.
I am running the project from IntelliJ IDEA. I don't know if the project gets the error also from the command line. The error occurs in both the LogIn interface and it also occurs when just using the main method to run what gets me the error.
Related Classes:
DatabaseConfig:
package Database_Managment;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class DatabaseConfig {
private static HikariDataSource dataSource;
static {
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://localhost:3306/storefront");
try {
Properties accessProperties = new Properties();
accessProperties.load(new FileInputStream("C:\\Users\\andre\\IdeaProjects\\demo\\Project-Poster\\src\\user.properties"));
//prop. to access the host
config.setUsername(accessProperties.getProperty("username"));
config.setPassword(accessProperties.getProperty("password"));
} catch (IOException e) {
throw new RuntimeException(e);
}
config.setMaximumPoolSize(1); // Adjust the pool size based on your needs
config.setIdleTimeout(60000); // Connection idle time
config.setConnectionTimeout(30000); // Connection timeout
dataSource = new HikariDataSource(config);
}
public static DataSource getConnectionPool() {
return dataSource;
}
}
DBFunctionality:
package Database_Managment;
import Client.Person;
import Server.ServerView;
import com.mysql.cj.jdbc.MysqlDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public final class DBFunctionality {
static String checkUserQuery = "SELECT username FROM storefront.users WHERE username = ? AND password = ?";
public static void manageClientData(String username, String password, String iconId, String status, String profileLink, boolean isNew, Person person) {
if (isNew) {
var ds = new MysqlDataSource();
ds.setServerName("localhost");
ds.setPort(3306);
ds.setUser("And_GG");
ds.setPassword("Sample Password"); //Can't show the real password
try (Connection conn = ds.getConnection(); Statement st = conn.createStatement()) {
String[] joinedValues = {username, password, profileLink, iconId, status};
String insertQuery = "INSERT INTO storefront.users (username, password, link, img, status) VALUES (?, ?, ?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(insertQuery);
for (int i = 1; i <= joinedValues.length; i++) {
ps.setString(i, joinedValues[i - 1]);
}
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
new ServerView(person.getTimeJoined(), person);
}
public static boolean checkUser(String username, String password) {
try (Connection conn = DatabaseConfig.getConnectionPool().getConnection();
PreparedStatement ps = conn.prepareStatement(checkUserQuery)) {
ps.setString(1, username);
ps.setString(2, password);
var rs = ps.getResultSet();
if (rs.isBeforeFirst()) return false;
} catch (SQLException e) {
//Not handle
}
return true;
}
}
LogInView (Only the relevant part):
//Instances that will constantly be re-used by the method under them
EntityManager entityManager = DatabaseEM.getEm();
EntityTransaction transaction = entityManager.getTransaction();
//if the user doesn't exist, we do nothing, else, we get it from the db. and init. the ServerView
confirmButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (!DBFunctionality.checkUser(nameBar.getText(), linkLabel2.getText())) return;
String jpql = "SELECT user FROM User user WHERE user.username = ?1 AND user.password = ?2";
try {
transaction.begin();
var query = entityManager.createQuery(jpql, User.class);
query.setParameter(1, nameBar.getText());
query.setParameter(2, linkLabel2.getText());
var user = query.getSingleResult();
if (user != null) {
new ServerView(LocalTime.now(), new Person(user.getUsername(), user.getPassword(), user.getImg(), user.getStatus(), user.getLink(), false, false));
}
transaction.commit();
} catch (Exception ex) {
//not handle
}
}
});
This is my maven pom.xml file:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.21.Final</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>javax.transaction-api</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
</dependency>
</dependencies>
</project>
1 Answer 1
This seems to be a problem in IntellijIDEA itself. For some reason your slf4j is not added to classpath.
Try pressing the following two buttons: enter image description here
If it doesn't help, click [File -> Invalidate Caches... -> Restart] in the upper left corner