Dear fellow Java developers:
I am having problems running a very basic EJB3 application to run. The code is actually the source code from the Manning book, "EJB3 in Action" http://manning.com/panda/
I am using the JBoss code examples in Eclipse 3.3, and am stuck on the source code example for chapter 1. After deploying the EJB, I try to run the client code by right clicking on the class, and selecting, Run As --> Java Application, and I get the following error:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at ejb3inaction.client.HelloUserClient.main(HelloUserClient.java:19)
I have listed all of my code below. The hierarchy of my files is as follows:
eWorkspace (name of my workspace folder in eclipse)
--->EJBtest (Java Project Name)
--->ejbModule
--->ejb3inaction (root folder of package)
--->client (folder holding client class) --->HelloUserClient.java
--->example (folder holding interface, and EJB)--->HelloUser.java (Interface)
--->HelloUserBean.java (EJB)
I know the error is in the client code, but not sure what it is. The EJB is successfully deployed. The issue is that the class name in the line:
helloUser = (HelloUser) context.lookup("ejbModule/"
+ HelloUserBean.class.getSimpleName() + "/remote");
is incorrect, but I'm not sure why. I've tried using "EJBtest/ejbModule/" and I've tried using "eWorkspace/EJBtest/ejbModule", and I've tried leaving it out altogether and using just HelloUserBean.class.getSimpleName() + "/remote"); which also didn't work.
I also tried using the fully qualified class name (ejb3inaction.example.HelloUserBean) and (ejb3inaction.example.HelloUserBean.class), which also didn't work, as this time I was getting a compilation error. Sigh. I am using eclipse 3.3 and JBoss 4.2.2.
Can anyone tell me where I am going wrong?
Thanks in advance to all who reply.
Below is the code I am using in full:
package ejb3inaction.example;
import javax.ejb.Remote;
@Remote
public interface HelloUser {
public void sayHello(String name);
}
----------------------------------------------------
package ejb3inaction.example;
import javax.ejb.Stateless;
import ejb3inaction.example.HelloUser;
@Stateless
public class HelloUserBean implements HelloUser{
public void sayHello (String name) {
System.out.println("Hello " + name + " welcome to EJB3!");
}
}
-----------------------------------------
package ejb3inaction.client;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import ejb3inaction.example.HelloUser;
import ejb3inaction.example.HelloUserBean;
public class HelloUserClient {
private static HelloUser helloUser;
public static void main(String[] args) {
try {
Context context = new InitialContext();
helloUser = (HelloUser) context.lookup("ejbModule/"
+ HelloUserBean.class.getSimpleName() + "/remote");
helloUser.sayHello("Curious George");
} catch (NamingException e) {
e.printStackTrace();
}
}
}