Accessing EJB entity bean from the Java client application
i262666 Mar 1, 2016 1:51 PMHi, I am new to JBoss, so I would appreciate some help.
I developed a simple entity bean and see it being deployed on the server. It uses the JBoss provided default database. Next I developed a simple java application client. When selecting Rus AS Application I am getting the following error message indicating that the client is unable to connect to the remote EJB
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(NamingManager.java:673)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:324)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:361)
at javax.naming.InitialContext.lookup(InitialContext.java:428)
at com.titan.clients.Client_1.main(Client_1.java:21)
Here is my 2 interfaces and the class, witch are compiled without errors.
Interface CabinHomeRemote
package com.titan.clients;
import javax.ejb.EJBHome;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
public interface CabinHomeRemote extends EJBHome
{
public CabinRemote create(Integer id)
throws CreateException, RemoteException;
public CabinRemote findByPrimaryKey(Integer pk)
throws FinderException, RemoteException;
}
Interface CabinHomeRemote:
package com.titan.clients;
import javax.ejb.EJBHome;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
public interface CabinHomeRemote extends EJBHome
{
public CabinRemote create(Integer id)
throws CreateException, RemoteException;
public CabinRemote findByPrimaryKey(Integer pk)
throws FinderException, RemoteException;
}
Class for the client:
package com.titan.clients;
import com.sun.xml.internal.fastinfoset.sax.Properties;
import com.titan.clients.CabinHomeRemote;
import com.titan.clients.CabinRemote;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
public class Client_1
{
public static void main(String [] args)
{
try
{
Context jndiContext = getInitialContext();
Object ref = jndiContext.lookup("CabinHomeRemote");
// Object ref = jndiContext.lookup("java:comp/env/ejb/CabinHomeRemote");
CabinHomeRemote home = (CabinHomeRemote)
PortableRemoteObject.narrow(ref,CabinHomeRemote.class);
CabinRemote cabin_1 = home.create(new Integer(1));
cabin_1.setName("Master Suite");
cabin_1.setDeckLevel(1);
cabin_1.setShipId(1);
cabin_1.setBedCount(3);
Integer pk = new Integer(1);
CabinRemote cabin_2 = home.findByPrimaryKey(pk);
System.out.println(cabin_2.getName());
System.out.println(cabin_2.getDeckLevel());
System.out.println(cabin_2.getShipId());
System.out.println(cabin_2.getBedCount());
}
catch (java.rmi.RemoteException re){re.printStackTrace();}
catch (javax.naming.NamingException ne){ne.printStackTrace();}
catch (javax.ejb.CreateException ce){ce.printStackTrace();}
catch (javax.ejb.FinderException fe){fe.printStackTrace();}
}
public static Context getInitialContext() throws javax.naming.NamingException
{
return new InitialContext();
}
}
application-client.xml:
<?xml version="1.0" encoding="UTF-8"?>
<application-client version="6" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application-client_6.xsd">
<display-name>CabinClient</display-name>
</application-client>
Thanks