Newbie problem with context lookup
elinombrable Jul 18, 2009 4:50 AMIm new here so at first, hi to everybody.
Im starting with EJB and Jboss and i have solved different problems i had till now but i cant find a solution for this.
Im building an easy example with 1 easy bean with remote interface with 1 single method that returns a string saying hello world.
Im trying to access that bean remotely from a simple java client, get the message and print into a dialog.
Here is the code:
The EJB remote interface
package stateless;
import javax.ejb.Remote;
@Remote
public interface NewSessionRemote {
String getMessage();
}The EJB bean implementing the interface
package stateless;
import javax.ejb.Stateless;
@Stateless
public class NewSessionBean implements NewSessionRemote {
public String getMessage() {
return "Hellow World";
}
}The standalone client
package standalone;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import stateless.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
try {
InitialContext ctx = null;
Properties props = new Properties(System.getProperties());
props.put(Context.PROVIDER_URL,"192.168.0.14:1099"); //thats my computer network ip... just for testing purposes. Then i will use a separated file for loading properties
props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
props.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
ctx = new InitialContext(props);
NewSessionRemote testEJB = (NewSessionRemote) ctx.lookup("MiPrueba/NewSessionBean/remote");
String cadena= testEJB.getMessage();
JOptionPane.showMessageDialog(null,cadena);
} catch (NamingException nex) {
JOptionPane.showMessageDialog(null,nex.toString());
}
}
}Config files are empty. I just used annotations.
Im using NetBeans as IDE and when i execute my standalone client from NetBeans, everything goes right. A MessageBox showing "Hellow world" appears (the context.lookup returns a proxy) but when i execute the program outside the IDE, i get the following:
Exception in thread "main" java.lang.ClassCastException: javax.naming.Reference cannot be cast to stateless.NewSessionRemote at standalone.Main.main(Main.java:22)
And changing a bit the code so i can get the classes returned with:
Object testEJB = ctx.lookup("MiPrueba/NewSessionBean/remote");
JOptionPane.showMessageDialog(null,testEJB.getClass().toString());when i execute it inside the IDE i get a : class $Proxy2
but executing it outside the IDE i get: class javax.naming.Reference
Any kind of help is welcome and thx in advance. You make it easier for people whos starting with this ;)
PD: If you need more information just ask for it