English 中文(简体)
Glassfish v3 / JNDI entry cannot be found problems!
原标题:

I ve been having problems trying to call an EJB s method from a Java Application Client. Here is the code.

EJB Remote Interface

package com.test;

import javax.ejb.Remote;

@Remote
public interface HelloBeanRemote {

    public String sayHello();

}

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!

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):

INFO: Portable JNDI names for EJB HelloBeanExample : [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]

So I tried with the following entries but I got the same exception :

  1. java:global/Test/HelloBeanExample
  2. java:global/Test/HelloBeanExample!com.test.HelloBeanRemote
  3. ejb/HelloBean
  4. ejb/HelloBean#com.test.HelloBeanRemote

I m using Netbeans 6.8 and Glassfish v3!

最佳回答

Actually, your problem is not the lookup of the JNDI reference of your bean or you would get something like that:

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.

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.

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.

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.

问题回答

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.html you will find your aswers!

Regards

I have encountered the same problem. I googled the 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.html and it has an information that I already tried to resolve the issue.

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

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).





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签