Java Glassfish v3 / JNDI 入口找不到问题!
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2087938/
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
Glassfish v3 / JNDI entry cannot be found problems!
提问by rudymatos
I've been having problems trying to call an EJB's method from a Java Application Client. Here is the code.
我在尝试从 Java 应用程序客户端调用 EJB 的方法时遇到问题。这是代码。
EJB Remote Interface
EJB 远程接口
package com.test;
import javax.ejb.Remote;
@Remote
public interface HelloBeanRemote {
public String sayHello();
}
EJB
EJB
package com.test;
import javax.ejb.Stateless;
@Stateless (name="HelloBeanExample" , mappedName="ejb/HelloBean")
public class HelloBean implements HelloBeanRemote {
@Override
public String sayHello(){
return "hola";
}
}
Main class (another project)
主类(另一个项目)
import com.test.HelloBeanRemote;
import javax.naming.Context;
import javax.naming.InitialContext;
public class Main {
public void runTest()throws Exception{
Context ctx = new InitialContext();
HelloBeanRemote bean = (HelloBeanRemote)ctx.lookup("java:global/Test/HelloBeanExample!com.test.HelloBeanRemote");
System.out.println(bean.sayHello());
}
public static void main(String[] args)throws Exception {
Main main = new Main();
main.runTest();
}
}
Well, what is my problem? JNDI entry for this EJB cannot be found!
那么,我的问题是什么?找不到此 EJB 的 JNDI 条目!
java.lang.NullPointerException
at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:297)
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:271)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at testdesktop.Main.runTest(Main.java:22)
at testdesktop.Main.main(Main.java:31) Exception in thread "main" javax.naming.NamingException: Lookup failed for 'java:global/Test/HelloBeanExample!com.test.HelloBeanRemote' in SerialContext [Root exception is javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext [Root exception is java.lang.NullPointerException]]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:442)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at testdesktop.Main.runTest(Main.java:22)
at testdesktop.Main.main(Main.java:31) Caused by: javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext [Root exception is java.lang.NullPointerException]
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:276)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
... 3 more Caused by: java.lang.NullPointerException
at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:297)
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:271)
... 4 more Java Result: 1
I've trying with different JNDI entries but nothing works (I got this entries from NetBeans console):
我尝试了不同的 JNDI 条目,但没有任何效果(我从 NetBeans 控制台获得了这些条目):
INFO: Portable JNDI names for EJB HelloBeanExample : [java:global/Test/HelloBeanExample, java:global/Test/HelloBeanExample!com.test.HelloBeanRemote]
信息:EJB HelloBeanExample 的可移植 JNDI 名称:[java:global/Test/HelloBeanExample, java:global/Test/HelloBeanExample!com.test.HelloBeanRemote]
INFO: Glassfish-specific (Non-portable) JNDI names for EJB HelloBeanExample : [ejb/HelloBean, ejb/HelloBean#com.test.HelloBeanRemote]
信息:EJB HelloBean 示例的 Glassfish 特定(非便携式)JNDI 名称:[ejb/HelloBean, ejb/HelloBean#com.test.HelloBeanRemote]
So I tried with the following entries but I got the same exception :
所以我尝试了以下条目,但我得到了同样的例外:
- java:global/Test/HelloBeanExample
- java:global/Test/HelloBeanExample!com.test.HelloBeanRemote
- ejb/HelloBean
- ejb/HelloBean#com.test.HelloBeanRemote
- java:global/Test/HelloBeanExample
- java:global/Test/HelloBeanExample!com.test.HelloBeanRemote
- ejb/HelloBean
- ejb/HelloBean#com.test.HelloBeanRemote
I'm using Netbeans 6.8 and Glassfish v3!
我正在使用 Netbeans 6.8 和 Glassfish v3!
采纳答案by Pascal Thivent
Actually, your problem is notthe lookup of the JNDI reference of your bean or you would get something like that:
实际上,您的问题不是查找 bean 的 JNDI 引用,否则您会得到类似的信息:
Caused by: javax.naming.NameNotFoundException: ejb/HelloBean not found
No, here, I suspect a simple classpath problem, you're simply missing some jar on the classpath of your client project. With GlassFish v3, adding $GF_HOME/modules/gf-client.jar
should be enough as mentioned in How do I access a Remote EJB component from a stand-alone java client?in GlassFish's EJB FAQ (my understanding is that this jar is supposed to replace $GF_HOME/lib/appserv-rt.jar
which is there for compatibility reasons with GFv2). It is however important to refer the gf-client.jar
from the GlassFish installation directory or the jars declared in its manifest won't be found.
不,在这里,我怀疑一个简单的类路径问题,您只是在客户端项目的类路径上缺少一些 jar。使用 GlassFish v3,添加$GF_HOME/modules/gf-client.jar
应该足够了,如如何从独立 Java 客户端访问远程 EJB 组件?在 GlassFish 的 EJB 常见问题解答中(我的理解是,$GF_HOME/lib/appserv-rt.jar
出于与 GFv2 的兼容性原因,这个 jar 应该替换存在的那个)。但是,gf-client.jar
从 GlassFish 安装目录中引用 非常重要,否则将找不到在其清单中声明的 jar。
gf-client.jar
refers to many other .jars from the GlassFish installation directory so it is best to refer to it from within the installation directory itself rather than copying it (and all the other .jars) to another location.
gf-client.jar
引用 GlassFish 安装目录中的许多其他 .jar,因此最好从安装目录本身中引用它,而不是将它(以及所有其他 .jar)复制到另一个位置。
Once you'll have this fixed, you should be able to lookup your bean using the JNDI names that GlassFish outputs in the logs. I'd suggest to use the new portable global JNDI names from Java EE 6.
修复此问题后,您应该能够使用 GlassFish 在日志中输出的 JNDI 名称来查找您的 bean。我建议使用来自 Java EE 6 的新的可移植全局 JNDI 名称。
Just in case, the What is the syntax for portable global JNDI names in EJB 3.1?entry from the GlassFish EJB FAQ provides a nice summary of this new convention. And if you want more information, check out: http://blogs.oracle.com/MaheshKannan/entry/portable_global_jndi_names.
以防万一,EJB 3.1 中可移植全局 JNDI 名称的语法是什么?GlassFish EJB FAQ 中的条目很好地总结了这个新约定。如果您想了解更多信息,请查看:http: //blogs.oracle.com/MaheshKannan/entry/portable_global_jndi_names。
回答by rudymatos
I have found something! I think there is a "special way" to add and configure components using NetBeans! I've been reading the netbeans's tutorials from the web pages and apparently I was doing the things wrong! So, this is not a real solution for this problem but maybe if you read one of those http://netbeans.org/kb/trails/java-ee.htmlyou will find your aswers!
我发现了什么!我认为有一种“特殊方式”可以使用 NetBeans 添加和配置组件!我一直在从网页上阅读 netbeans 的教程,显然我做错了!所以,这不是这个问题的真正解决方案,但也许如果你阅读其中一个http://netbeans.org/kb/trails/java-ee.html你会找到你的答案!
Regards
问候
回答by Abraham
I have encountered the same problem. I googledthe web and followed tutorials from Oracle line by line to set up a standalone java client but still nothing works. I came across http://glassfish.java.net/javaee5/ejb/EJB_FAQ.htmland it has an information that I already tried to resolve the issue.
我遇到了同样的问题。我 在网上搜索并逐行遵循 Oracle 的教程来设置独立的 Java 客户端,但仍然没有任何效果。我遇到了http://glassfish.java.net/javaee5/ejb/EJB_FAQ.html并且它有我已经尝试解决问题的信息。
I'll just restart my IDE, clean and build the project; may be also create the project afresh. And who knows, when the moon is full, my code might run fine :) So might wanna try that too
我将重新启动我的 IDE,清理并构建项目;也可以重新创建项目。谁知道呢,当满月时,我的代码可能会运行良好 :) 所以可能也想尝试一下
回答by Abraham
My stand alone client finally run! The trick was, just restarting Netbeans IDE, undeploying the application and redeploying it. This should fix your problem(assuming there is no other error in your code and configuration).
我的独立客户端终于运行了!诀窍是,只需重新启动 Netbeans IDE,取消部署应用程序并重新部署它。这应该可以解决您的问题(假设您的代码和配置中没有其他错误)。