当我尝试启动我们的java服务器时,我得到了这个: java.lang.ClassNotFoundException:com.mysql.jdbc.Driver
但我在脚本所在的同一文件夹中有mysql-connector-java-5.1.22-bin.jar,并在运行脚本时进行此操作:
java -cp mysql-connector-java-5.1.22-bin.jar -jar AvatarServer.jar
但仍然得到这个。该怎么办?我使用的是CentOS服务器。
root@host [/home/nagoom/Server_Example]# java -cp mysql-connector-java-5.1.22-bin.jar -jar AvatarServer.jar
************************
** FIRING UP SERVER! **
************************
Running MySQLChatMessageSaver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at database.DatabaseConnection.connect(DatabaseConnection.java:13)
at items.ItemsDatabase.DownloadData(ItemsDatabase.java:35)
at items.ItemsDatabase.InitializeData(ItemsDatabase.java:24)
at network.SocketHandler.startListening(SocketHandler.java:84)
at server.AvatarServer.run(AvatarServer.java:44)
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at database.DatabaseConnection.connect(DatabaseConnection.java:13)
at gameInstances.RoomController.DownloadRoomData(RoomController.java:46)
at gameInstances.RoomController.InitializeRooms(RoomController.java:32)
at network.SocketHandler.startListening(SocketHandler.java:85)
at server.AvatarServer.run(AvatarServer.java:44)
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at database.DatabaseConnection.connect(DatabaseConnection.java:13)
at gameInstances.RoomController.LoadLockedSeatsForAll(RoomController.java:121)
at gameInstances.RoomController.InitializeRooms(RoomController.java:33)
at network.SocketHandler.startListening(SocketHandler.java:85)
at server.AvatarServer.run(AvatarServer.java:44)
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at database.DatabaseConnection.connect(DatabaseConnection.java:13)
at gameInstances.RoomController.DownloadApartmentData(RoomController.java:89)
at gameInstances.RoomController.InitializeRooms(RoomController.java:34)
at network.SocketHandler.startListening(SocketHandler.java:85)
at server.AvatarServer.run(AvatarServer.java:44)
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at database.DatabaseConnection.connect(DatabaseConnection.java:13)
at gameInstances.RoomController.LoadItemsForAllRooms(RoomController.java:160)
at gameInstances.RoomController.InitializeRooms(RoomController.java:35)
at network.SocketHandler.startListening(SocketHandler.java:85)
at server.AvatarServer.run(AvatarServer.java:44)
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at database.DatabaseConnection.connect(DatabaseConnection.java:13)
at clothes.ClothesDatabase.DownloadGraphicsData(ClothesDatabase.java:42)
at clothes.ClothesDatabase.InitializeGraphics(ClothesDatabase.java:31)
at network.SocketHandler.startListening(SocketHandler.java:86)
at server.AvatarServer.run(AvatarServer.java:44)
MySQL exception: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
MySQL exception: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at database.DatabaseConnection.connect(DatabaseConnection.java:13)
at moods.MoodsDatabase.DownloadData(MoodsDatabase.java:29)
at moods.MoodsDatabase.InitializeData(MoodsDatabase.java:16)
at network.SocketHandler.startListening(SocketHandler.java:89)
at server.AvatarServer.run(AvatarServer.java:44)
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at database.DatabaseConnection.connect(DatabaseConnection.java:13)
at robots.RobotHandler.downloadBots(RobotHandler.java:31)
at network.SocketHandler.startListening(SocketHandler.java:90)
at server.AvatarServer.run(AvatarServer.java:44)
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at database.DatabaseConnection.connect(DatabaseConnection.java:13)
at levels.LevelDatabase.DownloadData(LevelDatabase.java:21)
at network.SocketHandler.startListening(SocketHandler.java:91)
at server.AvatarServer.run(AvatarServer.java:44)
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at database.DatabaseConnection.connect(DatabaseConnection.java:13)
at network.SocketHandler.resetOnlineStatuses(SocketHandler.java:172)
at network.SocketHandler.startListening(SocketHandler.java:93)
at server.AvatarServer.run(AvatarServer.java:44)
答案 0 :(得分:4)
-jar
和-cp
是互斥的。 -jar
说:类路径是以下jar,以及其清单文件中引用的所有库。所以你必须使用:
java -cp mysql-connector-java-5.1.22-bin.jar;AvatarServer.jar com.foo.bar.AvatarMainClass
或者,如果您想使用-jar
运行应用程序,请参阅jar的清单文件中的mysql驱动程序jar,如the jar tutorial中所述。
答案 1 :(得分:2)
将mysql-connector-java-5.1.22-bin.jar
添加到AvatarServer.jar
中 MANIFEST.MF 文件中的类路径。示例:
Class-Path: mysql-connector-java-5.1.22-bin.jar