gpt4 book ai didi

java.lang.NoClassDefFoundError : Could not initialize class com. DatabaseHandler.DBHandler

转载 作者:行者123 更新时间:2023-11-29 15:43:37 25 4
gpt4 key购买 nike

我正在尝试通过 heroku 托管我的第一个后端服务器。我已经托管了一个 mysql 服务器(也在 heroku 上),我可以使用我的 java servlet 和 mysql 工作台从本地连接到该服务器。

我使用heroku的git集成将我的servlet推送到他们的服务器,据我所知,我的tomcat服务器正在运行。当我尝试访问我的某个网址时,我收到一条错误消息。

我认为唯一重要的是最后一个。

java.lang.NoClassDefFoundError:无法初始化类 com.DatabaseHandler.DBHandler。

据我所知,这是因为我的 session 处理程序的静态初始化失败而引起的。

我已经仔细检查了所有连接,确保我的 mysql 驱动程序正确,将 mysql 驱动程序添加到构建路径中,以防heroku 找不到它,以及许多其他事情。

我的大多数 Google 搜索都没有找到我已经尝试过的解决方案或与我的问题无关的答案。

文章 Controller

public class ArticleService {
Session session;

public ArticleService() {
this.session = DBHandler.getSessionFactory().openSession();
}

public List<Article> getAllArticles() {
List<Article> list = session.createQuery("from Article").list();
return list;
}

public ArticleModelView getArticlePage(int pageNumber, int itemsPerPage) {
ArticleModelView avm = new ArticleModelView();

Query q = session.createQuery("from Article");
q = q.setFirstResult(pageNumber*itemsPerPage);
q = q.setMaxResults(itemsPerPage);

avm.articles = q.list();
int temp = session.createQuery("from Article").list().size();
temp = temp % itemsPerPage == 0 ? temp/itemsPerPage - 1: temp/itemsPerPage;


avm.maxPageSize = temp;

return avm;
}
}

数据库处理程序

public class DBHandler
{
private static final SessionFactory sessionFactory;

static {
try {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

public static void shutdown() {
getSessionFactory().close();
}
}

hibernate 配置

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">*******?reconnect=true</property>
<property name="hibernate.connection.password">******</property>
<property name="hibernate.connection.username">******</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.dailybagel.user.resources.User"/>
<mapping class="com.dailybagel.article.resources.Article"/>
</session-factory>
</hibernate-configuration>

当我使用本地主机访问该 url 时,应用程序会正确 ping 托管的 mysql 服务器并返回该表的内容。

目标是让托管 servlet 执行同样的操作。

最佳答案

看来 Heroku 放错了 Hibernate 配置文件的位置,或者它丢失了,或者类似的情况。将配置变量添加到 session 处理程序可以解决该问题。

我真的不明白为什么它会放错地方,但希望这会对某人有所帮助!

   static {
try {
Properties prop= new Properties();
prop.setProperty("hibernate.connection.url", "*********");
prop.setProperty("hibernate.connection.username", "********");
prop.setProperty("hibernate.connection.password", "********");
prop.setProperty("dialect", "org.hibernate.dialect.MySQLInnoDBDialect");
sessionFactory = new AnnotationConfiguration()
.addPackage("com.dailybagel.user.resources")
.addPackage("com.dailybagel.article.resources")
.addAnnotatedClass(User.class)
.addAnnotatedClass(Article.class)
.addProperties(prop)
.buildSessionFactory();

} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

如果有人知道为什么会发生这种情况,我很想了解更多

关于java.lang.NoClassDefFoundError : Could not initialize class com. DatabaseHandler.DBHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57316006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com