我有新的项目要做,它的网页聊天应用程序包含websockets,每条发送的消息都应保存在MySQL数据库中。
所以,我通过一些教程制作了聊天应用程序,所有代码都是这样的。
ChatController.java
import org.hibernate.jpa.HibernatePersistenceProvider;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.spi.PersistenceProvider;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@ServerEndpoint("/ws")
public class ChatServer {
private Map<String, String> usernames = new HashMap<String, String>();
//----Here is my problem
public PersistenceProvider persistenceProvider = new HibernatePersistenceProvider();
private EntityManager entityManager = persistenceProvider.
createEntityManagerFactory("NewPersistenceUnit", new HashMap()).createEntityManager();
//----Up there
@OnOpen
public void open(Session session) throws IOException, EncodeException {
session.getBasicRemote().sendText("(Server): Welcome to the chat room. Please state your username to begin.");
}
@OnClose
public void close(Session session) throws IOException, EncodeException {
String userId = session.getId();
if (usernames.containsKey(userId)) {
String username = usernames.get(userId);
usernames.remove(userId);
for (Session peer : session.getOpenSessions())
peer.getBasicRemote().sendText("(Server): " + username + " left the chat room.");
}
}
@OnMessage
public void handleMessage(String message, Session session) throws IOException, EncodeException {
String userId = session.getId();
if (usernames.containsKey(userId)) {
String username = usernames.get(userId);
//dbConn.addMessage(username,message);
for (Session peer : session.getOpenSessions()) {
peer.getBasicRemote().sendText("(" + username + "): " + message);
}
} else {
if (usernames.containsValue(message) || message.toLowerCase().equals("server"))
session.getBasicRemote().sendText("(Server): That username is already in use. Please try again.");
else {
usernames.put(userId, message);
session.getBasicRemote().sendText("(Server): Welcome, " + message + "!");
for (Session peer : session.getOpenSessions())
if (!peer.getId().equals(userId))
peer.getBasicRemote().sendText("(Server): " + message + " joined the chat room.");
}
}
}
}
的index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Chat</title>
</head>
<body style="margin:0;">
<textarea title="Chat Log" id="log" readonly
style="display: block; width: 100%; height: 600px; resize: none; margin: 0; padding: 0; border: 0;"></textarea>
<input title="Chat Input" id="input" type="text" style="display: block; width: 100%; border-width: 1px 0 1px 0;"
autofocus/>
<script>
var ws = new WebSocket("ws://localhost:8080/LastChat_war_exploded/ws");
ws.onmessage = function (event) {
console.log(event.data);
document.getElementById("log").value += "[" + timestamp() + "] " + event.data + "\n";
}
document.getElementById("input").addEventListener("keyup", function (event) {
if (event.keyCode === 13) {
ws.send(event.target.value);
event.target.value = "";
}
});
function timestamp() {
var d = new Date(), minutes = d.getMinutes();
if (minutes < 10) minutes = '0' + minutes;
return d.getHours() + ':' + minutes;
}
</script>
</body>
</html>
persistance.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">
<persistence-unit name="NewPersistenceUnit">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.entities.MessagesEntity</class>
<properties>
<property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/test"/>
<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root"/>
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/test"/>
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
<property name="eclipselink.jdbc.url" value="jdbc:mysql://localhost:3306/test"/>
<property name="eclipselink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties>
</persistence-unit>
</persistence>
所以Websocket聊天工作正常,但是当我试图在ChatController中添加两行时,我最终会使用 java.lang.NoClassDefFoundError:org / hibernate / jpa / HibernatePersistenceProvider 错误
在我以前的应用程序中,我只需要连接MySQL thorugh JPA,这条线就像一个魅力。 我像以前一样得到了一切。 图书馆: JPA 2.1 休眠-的EntityManager:5.2.10 MySQL的连接器的Java-43年1月5日
我也使用Intellij IDEA 2017.2.1
我一直在寻找很长时间,最后放弃了。 请帮助我解决这个错误,但是如果你得到了我应该用什么来使这个项目工作的信息,也许还有一些其他语言或一些框架 - 不要害怕告诉:)
编辑: 完整
java.lang.NoClassDefFoundError: org/hibernate/jpa/HibernatePersistenceProvider
at com.euro24.chat.ChatServer.<init>(ChatServer.java:21)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at org.glassfish.tyrus.core.ReflectionHelper.getInstance(ReflectionHelper.java:865)
at org.glassfish.tyrus.core.DefaultComponentProvider.create(DefaultComponentProvider.java:60)
at org.glassfish.tyrus.core.ComponentProviderService.getInstance(ComponentProviderService.java:239)
at org.glassfish.tyrus.core.ComponentProviderService.getEndpointInstance(ComponentProviderService.java:300)
at org.glassfish.tyrus.server.TyrusServerEndpointConfigurator.getEndpointInstance(TyrusServerEndpointConfigurator.java:182)
at org.glassfish.tyrus.core.AnnotatedEndpoint$1.getEndpointInstance(AnnotatedEndpoint.java:149)
at org.glassfish.tyrus.core.ComponentProviderService.getInstance(ComponentProviderService.java:151)
at org.glassfish.tyrus.core.AnnotatedEndpoint.callMethod(AnnotatedEndpoint.java:494)
at org.glassfish.tyrus.core.AnnotatedEndpoint.onOpen(AnnotatedEndpoint.java:570)
at org.glassfish.tyrus.core.TyrusEndpointWrapper.onConnect(TyrusEndpointWrapper.java:766)
at org.glassfish.tyrus.core.TyrusWebSocket.onConnect(TyrusWebSocket.java:155)
at org.glassfish.tyrus.core.TyrusWebSocketEngine$TyrusConnection.<init>(TyrusWebSocketEngine.java:821)
答案 0 :(得分:0)
正如尼尔指出的那样,您的课程路径中没有课程。 检查IntelliJ中的模块设置,并查看它是否在libss文件夹中,或者使用它作为项目设置中的源添加,如下所示:
一般来说,请阅读CLASSPATH的含义,例如: https://docs.oracle.com/javase/tutorial/essential/environment/paths.html