java 如何为独立客户端的 Glassfish 3.1.2 设置 JNDI?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12002948/
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
How to setup JNDI for Glassfish 3.1.2 for a stand-alone client?
提问by salocinx
I try to connect from a stand-alone swing client (running in a separate JVM on the client machine) to the Glassfish server.
我尝试从独立的 Swing 客户端(在客户端机器上的单独 JVM 中运行)连接到 Glassfish 服务器。
I currently use the following settings from Netbeans and everything works just fine:
我目前使用 Netbeans 的以下设置,一切正常:
System.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
System.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
System.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
System.setProperty("org.omg.CORBA.ORBInitialHost", "192.168.1.3");
System.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
InitialContext context = new InitialContext();
But when I try to start the compiled client from the console by typing "java -jar client.jar" I get the following error:
但是,当我尝试通过键入“java -jar client.jar”从控制台启动已编译的客户端时,出现以下错误:
D:\workspace\gf-client\dist>java -jar gf-client.jar
17.08.2012 11:07:38 ch.client.core.ServerContext getInitialContext SCHWERWIEGEND: Cannot instantiate class: com.sun.enterprise.naming.SerialInitContextFactory javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialInitContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.init(Unknown Source)
at javax.naming.InitialContext.<init>(Unknown Source)
at ch.lawsuite.core.ServerContext.getInitialContext(ServerContext.java:2 7)
at ch.client.core.remote.Facades.initialize(Facades.java:68)
at ch.client.core.Client.main(Client.java:57) Caused by: java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialIni tContextFactory
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)
... 7 more Exception in thread "main" java.lang.NullPointerException
at ch.client.core.remote.Facades.initialize(Facades.java:69)
at ch.client.core.Client.main(Client.java:57)
Somebody any helpful idea?
有人有任何有用的想法吗?
- Does the JVM miss any libs? Which ones? (it works from netbeans and all dependent libs are packed to the compiled jar-file (at least I think so..))
- Are there alternative serial context factories for Glassfish ?
- JVM 是否缺少任何库?哪个?(它适用于 netbeans,并且所有依赖库都打包到编译的 jar 文件中(至少我认为是这样......))
- Glassfish 是否有替代的串行上下文工厂?
Many thanks for your help in advance!
非常感谢您的帮助!
采纳答案by salocinx
By looking into the MANIFEST.MF file of the gf-client.jar library I noticed that there are some dozen other jar-libs referenced from there. In order to run the client outside of the netbeans platform, I had to copy all these libs to the final build of my own application. Then it works just fine... :-)
通过查看 gf-client.jar 库的 MANIFEST.MF 文件,我注意到从那里引用了几十个其他 jar-lib。为了在 netbeans 平台之外运行客户端,我必须将所有这些库复制到我自己的应用程序的最终构建中。然后它工作得很好...... :-)
回答by Sym-Sym
A point to clear about Remote EJB interfaces
需要明确远程 EJB 接口的一点
You want a Remote EJB interface when your client application lives on one JVM different than the one hosting the EJB module. In other words:
当您的客户端应用程序位于一个不同于托管 EJB 模块的 JVM 上时,您需要一个远程 EJB 接口。换句话说:
- The client application lives on one JVM and the EJB module is deployed on another JVM on the same machine
- 客户端应用程序位于一个 JVM 上,而 EJB 模块部署在同一台机器上的另一个 JVM 上
OR
或者
- The client application lives on one JVM and the EJB module is deployed on another JVM on a remote machine.
- 客户端应用程序位于一个 JVM 上,而 EJB 模块部署在远程机器上的另一个 JVM 上。
For simplicity sake
为简单起见
Lets consider the first scenario where both of the client app and the EJB module live on different JVMs on the very same machine.
让我们考虑第一个场景,其中客户端应用程序和 EJB 模块都位于同一台机器上的不同 JVM 上。
- Make sure you have 2 JDKs installed on your machine.
- Deploy the EJB module in a Glassfish installation pointing to one JDK (JVM). We can agree to install Glassfish in "C:/glassfish3/"
- According to the documantation in this link. Add to your client application classpath the file "gf-client.jar" as an external library from within the installation directory (i.e. C:/glassfish3/glassfish/lib/gf-client.jar) rather than copying it.
- Also add to your client application classpath the file remote_interface.jar includes the Remote business interface of your EJB.
- Run the SE (stand alone) client application on the second JDK (JVM)
- 确保您的机器上安装了 2 个 JDK。
- 在指向一个 JDK (JVM) 的 Glassfish 安装中部署 EJB 模块。我们可以同意在“C:/glassfish3/”中安装 Glassfish
- 根据此链接中的文档。将文件“gf-client.jar”作为安装目录(即 C:/glassfish3/glassfish/lib/gf-client.jar)内的外部库添加到您的客户端应用程序类路径,而不是复制它。
- 还要将文件remote_interface.jar 添加到您的客户端应用程序类路径中,该文件包括您的EJB 的远程业务接口。
- 在第二个 JDK (JVM) 上运行 SE(独立)客户端应用程序
An important tip about the client
关于客户的重要提示
As per the documentation, stand-alone java clients must explicitly use the global JNDI name to lookup the Remote EJB. Also, Glassfish does not need any properties instialization to invoke the InitialContext() constructor. Thus the client application can invoke the EJB using the following snippet:
根据文档,独立 java 客户端必须显式使用全局 JNDI 名称来查找远程 EJB。此外,Glassfish 不需要任何属性初始化来调用 InitialContext() 构造函数。因此,客户端应用程序可以使用以下代码段调用 EJB:
InitialContext context = new InitialContext();
_RemoteEjbInterface ejbBean = (_RemoteEjbInterface) context.lookup("java:global/DeployedEJBAppName/EjbImplClass!com.sam._RemoteEjbInterface");
In case your SE stand alone client application is a maven one then you need to
如果您的 SE 独立客户端应用程序是 Maven 应用程序,那么您需要
Account for the above step (3) by adding this entry to your client app POM:
<dependency> <groupId>org.glassfish.main.appclient.client</groupId> <artifactId>gf-client</artifactId> <version>3.1.2</version> <scope>system</scope> <systemPath>C:/glassfish3/glassfish/lib/gf-client.jar</systemPath> </dependency>
Account for the above step (4) by a POM dependency pointing to you remote_interface.jar in your local maven repo assuming you installed it. Follow this to know how.
通过将此条目添加到您的客户端应用程序 POM 来说明上述步骤 (3):
<dependency> <groupId>org.glassfish.main.appclient.client</groupId> <artifactId>gf-client</artifactId> <version>3.1.2</version> <scope>system</scope> <systemPath>C:/glassfish3/glassfish/lib/gf-client.jar</systemPath> </dependency>
假设您安装了本地 maven 存储库中的指向您的 remote_interface.jar 的 POM 依赖项,请考虑上述步骤 (4)。按照这个来了解如何。
Another documentation to reference is here
另一个参考文档在这里
回答by JamesB
You are missing the jar containing the class com.sun.enterprise.naming.SerialInitialContextFactory in your classpath. Add it to the manifest of your client jar.
您在类路径中缺少包含 com.sun.enterprise.naming.SerialInitialContextFactory 类的 jar。将其添加到客户端 jar 的清单中。