title: EJB 3.1 Problems with JNDI Stand-Alone Application with GlassFish 4
body: <p>I'm having a problem with the JNDI section of a stand-alone test application. I have the <code>gf-client.jar</code> and the <code>jndi-properties.jar</code> file on the class path. I made sure that the latest version was deployed on the glassfish server before running the client as well.</p>
  
  <p>Stand-Alone Application:</p>
  
  <pre><code>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"));
  }
  
  }
  </code></pre>
  
  <p>EJB 3.1 Application:</p>
  
  <pre><code>@Stateless(name = "java:global/server/TestBean")
  public class TestBean implements TestBeanLocal, TestBeanRemote {
  
  @Override
  public String sayHello(String name) {
      return "Hello " + name + "!";
  }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>@Remote
  public interface TestBeanRemote {
  
  String sayHello(String name);
  }
  </code></pre>
  
  <p>Exception: </p>
  
  <pre><code>    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.&lt;init&gt;(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
  </code></pre>
  
  <p>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 <a href="https://glassfish.java.net/javaee5/ejb/EJB_FAQ.html" rel="nofollow">https://glassfish.java.net/javaee5/ejb/EJB_FAQ.html</a> and, according to it, no arguments are required on the call to the constructor of <code>InitialContext</code> so this  behavior is mysterious to me. I'm using the NetBeans 8 ide and I have the files <code>gf-client.jar</code> and <code>jndi-properties.jar</code> appended to the project using the project/properties/libraries tab. NOTE: these jars are appended to the stand alone client only.</p>
  
  <p>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: </p>
  
  <pre><code>  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)
  </code></pre>
  
  <p>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.</p>
  
  <p>EDIT 12/6/14:
  Tried the tutorial, but I got:</p>
  
  <pre><code>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.&lt;init&gt;(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
  </code></pre>
  
  <p>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: <a href="https://www.dropbox.com/sh/cp6jho1bmhqczpw/AAAH7eWVwWJAxa99kg9DL-qJa" rel="nofollow">https://www.dropbox.com/sh/cp6jho1bmhqczpw/AAAH7eWVwWJAxa99kg9DL-qJa</a></p>