title: How do I run a simple EJB? I have a simple client, and a simple Session Bean created via Eclipse
body: <p>I'm a newbie to Java and to EJB. Want to create a simple EJB.</p>
  
  <p>So I've created an EJB Project via Eclipse (Eclipse Java EE IDE Juno Service Release 1 Build ID: 20120920-0800). Then I've added a Stateless Session Bean there with Remote and Local interfaces. Then I added a simple method that just returns a sum of 2 numbers. Here is the code:</p>
  
  <p>Test.java:</p>
  
  <pre><code>package ejb;
  
  import javax.ejb.Stateless;
  import javax.ejb.TransactionManagement;
  import javax.ejb.TransactionManagementType;
  
  /**
   * Session Bean implementation class Test
   */
  @Stateless
  @TransactionManagement(TransactionManagementType.BEAN)
  public class Test implements TestRemote, TestLocal {
  
  /**
   * Default constructor. 
   */
  public Test() {
      // TODO Auto-generated constructor stub
  }
  
  public int getSum(int a , int b) {
      return (a + b);
  }
  
  }
  </code></pre>
  
  <p>TestLocal.java:</p>
  
  <pre><code>package ejb;
  
  import javax.ejb.Local;
  
  @Local
  public interface TestLocal {
      public int getSum(int a , int b);
  }
  </code></pre>
  
  <p>TestRemote.java :</p>
  
  <pre><code>package ejb;
  
  import javax.ejb.Remote;
  
  @Remote
  public interface TestRemote {
      public int getSum(int a , int b);
  }
  </code></pre>
  
  <p>I right-clicked on my EJB Project and exported it as a JAR file. Then I deployed it to GlassFish3 server: 1) Applications - Deploy and so on; 2) JNDI - Custom Resources. I've set Resource Type to <code>ejb.TestRemote</code> , Factory Class to <code>org.glassfish.resources.custom.factory.EjbFactory</code> . JNDI Name was <code>Test</code>. Then I've coded a simple client:</p>
  
  <p>Main.java:</p>
  
  <pre><code>import javax.naming.InitialContext;
  import ejb.TestRemote;
  
  public class Main {
      public static void main(String arg[]) {
          try {           
              TestRemote testR = (TestRemote) new InitialContext().lookup("Test");
              //System.out.println(new InitialContext().lookup("Test").toString());
              //.toString displays: Reference Class Name: ejb.TestRemote
              System.out.println(testR.getSum(50, 500));
          } catch(Exception e) {
              e.printStackTrace();
          }
      }   
  }
  </code></pre>
  
  <p>I've added <code>gf-client-module.jar</code> from <code>C:\glassfish3\glassfish\modules</code> and a JAR file of my EJB Project to Java Build Path (Project-Properties).</p>
  
  <p>Then I tried to launch my client in Eclipse and got following message appeared immediately:</p>
  
  <pre><code>com.sun.enterprise.v3.server.CommonClassLoaderServiceImpl findDerbyClient
  INFO: Cannot find javadb client jar file, derby jdbc driver will not be available by default.
  </code></pre>
  
  <p>Then I got next message:</p>
  
  <pre><code>java.lang.ClassCastException: javax.naming.Reference cannot be cast to ejb.TestRemote
  at Main.main(Main.java:16)
  </code></pre>
  
  <p>So, can anyone please help me with that?.. I'm really confused: .toString method called on a reference displays that it is a proper ejb.TestRemote class reference, as I mentioned before. But... It doesn't work ((</p>
  
  <p>Any ideas? Thanks in advance.</p>
  
  <p><strong>RESOLVED FOR ME, BUT WITH SOME ANOTHER WAY</strong></p>
  
  <p>I've lost hope to make it with Eclipse and separately installed GlassFish. So, I've installed a GlassFish server to Eclipse, and deployed my EJB project there. It worked: now my Client works fine. The only change was - I should provide a full package name to .lookup: like <code>.lookup("ejb.TestRemote")</code>.</p>
  
  <p>Thanks everyone who tried/wanted/wished to help!</p>