• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 944
  • Last Modified:

Having problems running a basic EJB3 application using Eclipse 3.3 and JBoss 4.2

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

Can anyone tell me where I am going wrong?

Thanks in advance to all who reply.


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();
		}
	}
}

Open in new window

0
fsyed
Asked:
fsyed
  • 5
  • 2
1 Solution
 
Gibu GeorgeCTOCommented:
Which version JBoss are you using?
0
 
ramazanyichCommented:
it is indeed problem in your initialcontext initialization.
You should put some properties to the context to point to correct jboss server.
Properties env = new Properties();
env.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
env.setProperty("java.naming.provider.url", "localhost:1099");
env.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
Context context = new InitialContext(env);

You just need to adjust hostname:port (localhost:1099) if you are running on non-default config.

0
 
fsyedAuthor Commented:
gibu_george:I am using JBoss version 4.2 .

ramazanyich:I tried to use your code, but unfortunately I am now getting the following error message:

javax.naming.NameNotFoundException: ejbModule not bound
      at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
      at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
      at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
      at org.jnp.server.NamingServer.lookup(NamingServer.java:267)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
      at sun.rmi.transport.Transport$1.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.rmi.transport.Transport.serviceCall(Unknown Source)
      at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
      at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
      at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
      at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
      at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
      at sun.rmi.server.UnicastRef.invoke(Unknown Source)
      at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:667)
      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:627)
      at javax.naming.InitialContext.lookup(Unknown Source)
      at ejb3inaction.client.HelloUserClient.main(HelloUserClient.java:26)


My code looks like this now:

Properties env = new Properties();
                  env.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
                  env.setProperty("java.naming.provider.url", "localhost:1099");
                  env.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
                  
                  Context context = new InitialContext();
                  helloUser = (HelloUser) context.lookup("ejbModule/"
                              + HelloUserBean.class.getSimpleName() + "/remote");
                  helloUser.sayHello("Curious George");

I have no reason to believe I am not using non-default settings, as when I go to http://localhost:8080, I see the "Welcome to JBoss" page.  As I said, I think it has something to with the line:

helloUser = (HelloUser) context.lookup("ejbModule/"
                              + HelloUserBean.class.getSimpleName() + "/remote");

Thanks again for all of your help.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
fsyedAuthor Commented:
Another point.  When I modify the code to look like this:

Properties env = new Properties();
                  env.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
                  env.setProperty("java.naming.provider.url", "localhost:1099");
                  env.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
                  
                  Context context = new InitialContext(env);
                  helloUser = (HelloUser) context.lookup(HelloUserBean.class.getSimpleName());
                  helloUser.sayHello("Curious George");

I now get the following error message:

Exception in thread "main" java.lang.ClassCastException: org.jnp.interfaces.NamingContext cannot be cast to ejb3inaction.example.HelloUser
      at ejb3inaction.client.HelloUserClient.main(HelloUserClient.java:26)

I'm trying everything I can to correct this.  Thanks again for all of your help.
0
 
Gibu GeorgeCTOCommented:
Version is 4.2 or 4.2.2?
0
 
fsyedAuthor Commented:
4.2.2
0
 
fsyedAuthor Commented:
Hi:

I'm still having problems with this issue, and I also realized I made a slight error in my original post above.  My folder hierarchy 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)

This is the correct hierarchy, and as I said above, I am still having the errors mentioned above when I try to run the client.

Thanks in advance to all who reply.
0
 
fsyedAuthor Commented:
Thanks ramazanyich for your answer.  Your solution helped me correct my problem.  My other issue was that I was not deploying the EJB corectly, which is why I was getting the last error message.  Thanks so much for your patience!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now