EJB 3.1 Problems with JNDI Stand-Alone Application with GlassFish 4

◇◆丶佛笑我妖孽 提交于 2019-12-12 20:35:17

问题


I'm having a problem with the JNDI section of a stand-alone test application. I have the gf-client.jar and the jndi-properties.jar file on the class path. I made sure that the latest version was deployed on the glassfish server before running the client as well.

Stand-Alone Application:

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws NamingException {
    Context context = new InitialContext();
    TestBeanRemote bean = (TestBeanRemote) context.lookup("java:global/server/TestBean");
    System.out.println(bean.sayHello("Sarah"));
}

}

EJB 3.1 Application:

@Stateless(name = "java:global/server/TestBean")
public class TestBean implements TestBeanLocal, TestBeanRemote {

@Override
public String sayHello(String name) {
    return "Hello " + name + "!";
}
}

@Remote
public interface TestBeanRemote {

String sayHello(String name);
}

Exception:

    Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.impl.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.enterprise.naming.impl.SerialInitContextFactory]
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:674)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
    at javax.naming.InitialContext.init(InitialContext.java:244)
    at javax.naming.InitialContext.<init>(InitialContext.java:192)
    at com.gmail.physicistsarah.servertestclient.core.Main.main(Main.java:23)
Caused by: java.lang.ClassNotFoundException: com.sun.enterprise.naming.impl.SerialInitContextFactory
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:340)
    at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:72)
    at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:61)
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:672)
    ... 4 more
    Java Result: 1

EDIT: Well it seems that it actually breaks on the very first line when the initial context is declared. I'm following the instructions on https://glassfish.java.net/javaee5/ejb/EJB_FAQ.html and, according to it, no arguments are required on the call to the constructor of InitialContext so this behavior is mysterious to me. I'm using the NetBeans 8 ide and I have the files gf-client.jar and jndi-properties.jar appended to the project using the project/properties/libraries tab. NOTE: these jars are appended to the stand alone client only.

EDIT2: I created a new client project referencing the server project in it's properites, and now I'm getting a new exception when run:

  C:\Users\PC\Documents\NetBeansProjects\Server Client\nbproject\build-impl.xml:990: C:\Users\PC\GlassFish_Server\glassfish\domains\Server\generated\xml\Server Client does not exist.
at org.apache.tools.ant.types.AbstractFileSet.getDirectoryScanner(AbstractFileSet.java:484)
at org.apache.tools.ant.taskdefs.Copy.execute(Copy.java:487)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.GeneratedMethodAccessor197.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1393)
at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1248)
at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:286)
at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:555)
at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:153)

The setup is: the server client project referencing the server project (running same client code above) and the server project which is running at the time of the client's call to run.

EDIT 12/6/14: Tried the tutorial, but I got:

Exception in thread "main" java.lang.RuntimeException: javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.impl.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.enterprise.naming.impl.SerialInitContextFactory]
at test.java.application.TestJavaApplication.main(TestJavaApplication.java:28)
Caused by: javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.impl.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.enterprise.naming.impl.SerialInitContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:674)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.init(InitialContext.java:244)
at javax.naming.InitialContext.<init>(InitialContext.java:192)
at test.java.application.TestJavaApplication.main(TestJavaApplication.java:23)
Caused by: java.lang.ClassNotFoundException: com.sun.enterprise.naming.impl.SerialInitContextFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:340)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:72)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:61)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:672)
... 4 more
Java Result: 1

The strange thing is that it's not saying that it couldn't find the name or anything, it's saying that it can't find a certain class, which I guess is a part of JNDI. The instructions were followed to the letter. I'm going to post all the code that I used. I'm just about at the point that I'm going to do a clean install of NetBeans and GlassFish. Link: https://www.dropbox.com/sh/cp6jho1bmhqczpw/AAAH7eWVwWJAxa99kg9DL-qJa


回答1:


I have implemented a similar standalone client using Netbeans and GlassFish for a Java EE workshop and used the following code for it:

    InitialContext ctx = new InitialContext();
    GreetingBeanRemote ref = (GreetingBeanRemote) ctx.lookup("java:global/JWS2_JEE5_Server/JWS2_JEE5_Server-ejb/GreetingBean");
    String result = ref.greet("Hello");

Note the JNDI name, which uses the format java:global/<app-name>/<module-name>/<bean-name>, which might help you using the correct name, if that ist the problem. Another thing to try in this regard is using the mappedName attribute int the @Stateless annotation (e.g. @Stateless(mappedName="MySessionBean")), which works for GlassFish.

Concerning your exception, it suggests that the naming itself is not the problem, but more the classpath or the artefact you deployed. My setup in Netbeans is as follows:

  • Client project, containing the standalone client
  • Server project, containing the EJB implementation
  • Lib project, containing the EJB remote interface
  • Client project is referencing the lib project
  • Lib project is referencing the javaee-api jar

Maybe the information helps in setting it up correctly.


EDIT 14/07/11:

Here's a step-by-step tutorial on how to create the above scenario with Netbeans (8):

Requirements: Netbeans EE edition (8) with GlassFish 4 installed, JDK 1.7 or above

  1. New project/Java EE/Enterprise Application: "MyEnterpriseApplication", create EJB module, Java EE 6, GlassFish 4
  2. New project/Java/Java Class Library: "MyJavaLibrary"
  3. New project/Java/Java Application: "MyJavaApplication"
  4. in MyJavaApplication: Properties/Libraries/Add project: MyJavaLibrary, Add JAR: gf-client.jar from GlassFish installation lib folder
  5. in MyEnterpriseApplication-ejb: New SessionBean "HelloBean", create remote interface, select project MyJavaLibrary
  6. in MyJavaLibrary: edit "HelloBeanRemote", add [1]
  7. in MyEnterpriseApplication-ejb:edit "HelloBean", add [2]
  8. in MyJavaApplication: edit "MyJavaApplication", add [3] in main
  9. run MyEnterpriseApplication -> check correct deployment in GF console output
  10. run MyJavaApplication -> see successful call in console output

[1]

String sayHello(String name);

[2]

@Override
public String sayHello(String name) {
   return "Hello " + name;
}

[3]

try {
    InitialContext context = new InitialContext();           
    HelloBeanRemote helloBean =
        (HelloBeanRemote) context.lookup("java:global/MyEnterpriseApplication/MyEnterpriseApplication-ejb/HelloBean");
    System.out.println(helloBean.sayHello("Geziefer"));
} catch (NamingException ex) {          
}



回答2:


I had to import these many jars exactly to make my standalone client access my remote EJB !!!

javaee.jar

gf-client.jar

jndi-properties.jar

Your EJB jar file : EJBApp.jar

appserv-rt.jar

config-api.jar

config-types.jar

pfl-basic.jar

pfl-basic-tools.jar

connectors-internal-api.jar

container-common.jar

deployment-common.jar

dol.jar

ejb.security.jar

ejb-container.jar

glassfish-api.jar

glassfish-corba-csiv2-idl.jar

glassfish-corba-internal-api.jar

glassfish-corba-omgapi.jar

glassfish-corba-orb.jar

glassfish-naming.jar

gmbal.jar

hk2-core.jar

internal-api.jar

javax.ejb-api.jar

javax.jms-api.jar

javax.resource-api.jar

javax.servlet-api.jar

javax.transaction-api.jar

jta.jar

kernel.jar

management-api.jar

orb-connector.jar

orb-iiop.jar

security.jar

transaction-internal-api.jar

hk2.jar

hk2-api.jar

hk2-config.jar

hk2-locator.jar

hk2-runlevel.jar

hk2-utils.jar

javax.inject.jar

common-util.jar

validation-api-1.1.0.Final (1).jar

orb-enabler.jar

pfl-asm.jar

pfl-dynamic.jar

pfl-tf.jar

pfl-tf-tools.jar

ssl-impl.jar

glassfish.jar

gms-adapter.jar

gms-bootstrap.jar



来源:https://stackoverflow.com/questions/24582575/ejb-3-1-problems-with-jndi-stand-alone-application-with-glassfish-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!