mongodb 如何在 glassfish 中使用 3rd 方库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2507099/
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-09-09 11:43:24  来源:igfitidea点击:

How to use 3rd party libraries in glassfish?

mongodbglassfishjava-ee-6glassfish-3

提问by Hank

I need to connect to a MongoDB instance from my EJB3 application, running on glassfish 3.0.1. The Mongo project provides a set of drivers, and I'm able to use them in a standalone Java application.

我需要从运行在 glassfish 3.0.1 上的 EJB3 应用程序连接到 MongoDB 实例。Mongo 项目提供了一组驱动程序,我可以在独立的 Java 应用程序中使用它们。

How would I use them in a Java EE application? Or maybe better phrasing: how would I make a 3rd party library available to my application when it runs in an EJB container?

我将如何在 Java EE 应用程序中使用它们?或者也许更好的措辞:当我的应用程序在 EJB 容器中运行时,我如何使它可以用于我的应用程序?

At the moment, I'm getting a java.lang.NoClassDefFoundError when deploying a bean that tries to import from the library:

目前,在部署尝试从库中导入的 bean 时,我收到 java.lang.NoClassDefFoundError 错误:

[#|2010-03-24T11:42:15.164+0100|SEVERE|glassfishv3.0|global|_ThreadID=28;_ThreadName=Thread-1;|Class [ com/mongodb/DBObject ] not found. Error while loading [ class mvs.core.LocationCacheService ]|#]
[#|2010-03-24T11:42:15.164+0100|WARNING|glassfishv3.0|javax.enterprise.system.tools.deployment.org.glassfish.deployment.common|_ThreadID=28;_ThreadName=Thread-1;|Error in annotation processing: java.lang.NoClassDefFoundError: com/mongodb/DBObject|#]

[#|2010-03-24T11:42:15.259+0100|SEVERE|glassfishv3.0|javax.enterprise.system.core.com.sun.enterprise.v3.server|_ThreadID=28;_ThreadName=Thread-1;|Exception while loading the app
org.glassfish.deployment.common.DeploymentException: java.lang.NoClassDefFoundError: com/mongodb/DBObject
at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:171)
at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:125)
at org.glassfish.internal.data.ApplicationInfo.load(ApplicationInfo.java:224)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:338)

I tried adding it to the NetBeans project (Properties -> Libraries -> Compile -> Add Jar, enable 'Package'), and I also tried manually copying the jar file to $GF_HOME/glassfish/domains/domain1/lib (where the mysql-connector already resides).

我尝试将它添加到 NetBeans 项目(属性 -> 库 -> 编译 -> 添加 Jar,启用“包”),我还尝试将 jar 文件手动复制到 $GF_HOME/glassfish/domains/domain1/lib(其中mysql-connector 已经存在)。

Do I need to 'register' the library with the container? Reference it via Annotation? Extend the classpath of the container to include the library?

我需要在容器中“注册”库吗?通过注释引用它?扩展容器的类路径以包含库?

回答by Pascal Thivent

Hmm... Shouldn't you put this "driver" in glassfishv3/glassfish/domains/domain1/lib/ext?

嗯...你不应该把这个“驱动程序”放进去glassfishv3/glassfish/domains/domain1/lib/ext吗?

回答by Sebastien Lorber

You could put shared libs to lib/ext of your domain. commons-logging and jdbc drivers are often added in this domain path.

您可以将共享库放入域的 lib/ext 中。commons-logging 和 jdbc 驱动程序通常会添加到此域路径中。

Common Class Loader

GlassFish v2 has a well defined Class Loader hierarchy which identifies the common class loader as the proper way to deal with shared libraries. So to make a long story short, putting you libraries and other framework JARs in domains/domain1/lib is all you need to do.

lib/, not lib/ext

The person asking me the question had tried putting the libraries in domains/domain1/lib/ext which triggered an interesting ClassNotFoundError for core Java EE classes such as javax.servlet.http.HttpServlet. Shing Wai Chan was quick to explain that domains/domain1/lib/ext is part of -Djava.ext.dirs which makes any of its JARs be considered as a JDK extension which means web app frameworks placed there will be loaded before webcontainer implementation classes as they are higher up in the classloader delegation chain.

通用类加载器

GlassFish v2 有一个明确定义的类加载器层次结构,它将公共类加载器标识为处理共享库的正确方法。因此,长话短说,只需将库和其他框架 JAR 放在 domain/domain1/lib 中即可。

lib/,不是 lib/ext

问我这个问题的人曾尝试将库放在 domain/domain1/lib/ext 中,这触发了核心 Java EE 类(例如 javax.servlet.http.HttpServlet)的有趣 ClassNotFoundError。Shing Wai Chan 很快解释说,domains/domain1/lib/ext 是 -Djava.ext.dirs 的一部分,这使得它的任何 JAR 都被视为 JDK 扩展,这意味着放置在那里的 web 应用程序框架将在 webcontainer 实现类之前加载因为它们在类加载器委托链中处于更高的位置。

回答by aashish

Glassfish has own Class loader hierarchy, http://docs.oracle.com/cd/E19798-01/821-1752/beade/index.html
I face the same problem in my project and then I put all my Third party libraries in domain/domain1/lib and my problem solved. On other way round, my problem was solved too by putting libraries in glassfish/lib.

Glassfish 有自己的类加载器层次结构,http://docs.oracle.com/cd/E19798-01/821-1752/beade/index.html
我在我的项目中遇到同样的问题,然后我把我所有的第三方库放在domain/domain1/lib 和我的问题解决了。另一方面,通过将库放在 glassfish/lib 中也解决了我的问题。

回答by Sergio

In my case I was using Oracle Express Edition 11gR2 and Glassfish 3.1.2 and the ONLY way that works in my case was putting the ojdbc6 in:

在我的情况下,我使用的是 Oracle Express Edition 11gR2 和 Glassfish 3.1.2,在我的情况下唯一有效的方法是将 ojdbc6 放入:

C:\Program Files\glassfish-3.1.2.2\glassfish\lib

回答by DimXenon

Try to put Your libs into $GF_HOME/glassfish/modules/. It's dirty, but will work.

尝试将您的库放入 $GF_HOME/glassfish/modules/。它很脏,但会起作用。

回答by Sangeeth

  1. Go to your Glassfish doamin directory.

  2. Then go to lib folder.

  3. Place the libraries there.

  4. Restart the glassfish and run.

  1. 转到您的 Glassfish doamin 目录。

  2. 然后转到lib文件夹。

  3. 将图书馆放在那里。

  4. 重新启动 glassfish 并运行。

(Ex) C:\glassfish3\glassfish\domains\domain1\lib

(例如) C:\glassfish3\glassfish\domains\domain1\lib