将 MySQL 数据库连接到 Glassfish 类路径未设置或类名错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8349970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:54:28  来源:igfitidea点击:

Connecting a MySQL database to Glassfish classpath is not set or classname is wrong

mysqleclipsejakarta-eeglassfish

提问by Randnum

I'm swapping out a derby database for a MySQL one. I had everything working before but after what I thought was the proper configuration I'm getting the error:

我正在将 derby 数据库换成 MySQL 数据库。我之前一切正常,但在我认为正确的配置之后,我收到了错误:

Caused by: javax.resource.ResourceException: Class name is wrong or classpath is not set for : com.mysql.jdbc.jdbc2.optional.MysqlDataSource

引起:javax.resource.ResourceException: Class name is wrong or classpath is not set for : com.mysql.jdbc.jdbc2.optional.MysqlDataSource

Full error output from console:

控制台的完整错误输出:

Caused by: javax.resource.ResourceException: Class name is wrong or classpath is not set for : com.mysql.jdbc.jdbc2.optional.MysqlDataSource
at com.sun.gjc.common.DataSourceObjectBuilder.getDataSourceObject(DataSourceObjectBuilder.java:292)
at com.sun.gjc.common.DataSourceObjectBuilder.constructDataSourceObject(DataSourceObjectBuilder.java:114)
at com.sun.gjc.spi.ManagedConnectionFactory.getDataSource(ManagedConnectionFactory.java:1292)
at com.sun.gjc.spi.DSManagedConnectionFactory.getDataSource(DSManagedConnectionFactory.java:148)
at com.sun.gjc.spi.DSManagedConnectionFactory.createManagedConnection(DSManagedConnectionFactory.java:101)
at com.sun.enterprise.resource.allocator.LocalTxConnectorAllocator.createResource(LocalTxConnectorAllocator.java:87)

I've double checked some of the names, the connection pool and other resources.I've also added the MySQL driver .jars to the library of glassfish in both projects. The database was definitely working correctly through eclipse because I was able to view tables and display the resources inside the database context of eclipse. So I know that at least THOSE drivers are working correcly. Also the persistence.xml file looks good. it references the jdbc/mydatabase jndi reference like it should and default JTA is selected as the manament type.

我已经仔细检查了一些名称、连接池和其他资源。我还在两个项目中将 MySQL 驱动程序 .jars 添加到 glassfish 库中。数据库肯定通过 eclipse 正常工作,因为我能够查看表并显示 eclipse 数据库上下文中的资源。所以我知道至少那些驱动程序工作正常。此外,persistence.xml 文件看起来不错。它像它应该的那样引用 jdbc/mydatabase jndi 引用,并且默认 JTA 被选为管理类型。

Does anyone have another suggestion? Thank you

有人有其他建议吗?谢谢

回答by BalusC

I've also added the MySQL driver .jars to the library of glassfish in both projects.

我还在两个项目中将 MySQL 驱动程序 .jars 添加到 glassfish 库中。

It was apparently not done right. The JAR has to go in /glassfish/domains/[domainname]/lib/extfolder of the Glassfish installation where [domainname]usually defaults to domain1. You can and should not configure it from the Eclipse side on.

这显然没有做对。JAR 必须/glassfish/domains/[domainname]/lib/ext放在 Glassfish 安装的文件夹中,[domainname]通常默认为domain1. 您可以也不应该从 Eclipse 端配置它。

回答by Ram

Looks like I am replying very late, but however people who would be referring to this thread may find the following information being useful. So I am posting it here:

看起来我回复得很晚,但是参考此线程的人可能会发现以下信息很有用。所以我把它贴在这里:

  1. Download the connector Jar from http://dev.mysql.com/downloads/connector/j/5.0.html
  2. unzip the pack and copy mysql-connector-java-verno-bin.jar
  3. past the same at [GlassFish Installation Directory]/domains/[domain name]/lib folder
  4. restart your domain and ping to check your connection in JDBC Connection Pools
  1. http://dev.mysql.com/downloads/connector/j/5.0.html下载连接器 Jar
  2. 解压缩包并复制 mysql-connector-java-verno-bin.jar
  3. [GlassFish 安装目录]/domains/[域名]/lib 文件夹中的过去相同
  4. 重新启动您的域并 ping 以检查您在 JDBC 连接池中的连接

There you go. If your MySql is running then it will ping the DB successfully

你去吧。如果您的 MySql 正在运行,那么它将成功 ping 数据库

回答by wei

I copied the jar file to $glassfish_install_folder\glassfish\lib, after that it worked. I use glassfish 4.0.

我将 jar 文件复制到$glassfish_install_folder\glassfish\lib,然后它就可以工作了。我使用glassfish 4.0

Check this linkfrom oracle.

从 oracle检查此链接

回答by Mercious

I encountered this issue in 2018 and would like to note that if you are using glassfish 4 (current is 5) then it seems you have to use the Connector/J 5.1.47 version for it to work. If you use the current version (Connector/J 8.0.13) then exception mentioned in the original question keeps appearing, no matter where you place the .jar file.

我在 2018 年遇到了这个问题,想指出的是,如果您使用的是 glassfish 4(当前为 5),那么您似乎必须使用 Connector/J 5.1.47 版本才能使其工作。如果您使用当前版本 (Connector/J 8.0.13),那么无论您将 .jar 文件放在哪里,原始问题中提到的异常都会不断出现。

With Connector/J 5.1.47 it works perfectly.

使用 Connector/J 5.1.47,它可以完美运行。

回答by helvete

I encountered this issue in 2019 and would like to note that if you are using docker image payara/server-full(I am using 5.194so far) as I do the location to place the driver jar is:

我在 2019 年遇到了这个问题,并想指出,如果您使用 docker 镜像payara/server-full(我目前正在使用5.194),那么放置驱动程序 jar 的位置是:

/opt/payara/appserver/glassfish/domains/production/lib/

/opt/payara/appserver/glassfish/domains/production/lib/

In the end I am doing something like this in the Dockerfileof payara server:

最后我在Dockerfilepayara服务器上做这样的事情:

RUN wget http://central.maven.org/maven2/org/mariadb/jdbc/mariadb-java-client/2.2.0/mariadb-java-client-2.2.0.jar \
    -O /opt/payara/appserver/glassfish/domains/production/lib/mariadb-java-client-2.2.0.jar