File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Calling EJB From the Client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Calling EJB From the Client" Watch "Calling EJB From the Client" New topic
Author

Calling EJB From the Client

naveen babu
Greenhorn

Joined: Nov 10, 2004
Posts: 8
I have created a simple entity bean using jboss.
i have deployed the entity bean.
now i want to call it from the client.
while running i get the error.

javax.naming.NoInitialCotextException Cannot instantiate class:jnp.interfaces.NamingContextFactory.

my client coding is
----------------------------------------------------------------------------
ackage product.myproduct;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.PortableRemoteObject;
import java.util.*;


public class ProductClient
{
public static void main(String args[])
{
ProductHome home=null;
System.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
System.setProperty("java.naming.provider.url", "192.168.1.7:1099");

try{
InitialContext jndiContext = new InitialContext();
home=(ProductHome)PortableRemoteObject.narrow(jndiContext.lookup("ProductHome"),ProductHome.class);

home.create("123-456-7890","P5-350","350 Mhz pentium");
/*home.create("123-456-7891","P5-400","400 Mhz pentium","300");
home.create("123-456-7892","P5-450","450 Mhz pentium","400");
home.create("123-456-7893","SD-64","64 MB SDRAM","50");
home.create("123-456-7894","SD-128","128 MB SDRAM","100");*/

Iterator i=home.findByName("SD-64").iterator();
System.out.println("These products match the name SD-64:");
while(i.hasNext())
{
Product prod=(Product)PortableRemoteObject.narrow(i.next(),Product.class);
System.out.println(prod.getDescription());
}

/*i=home.findByBasePrice("200").iterator();
System.out.println("These products match the base price $200");
while(i.hasNext())
{
Product prod=(Product) PortableRemoteObject.narrow(i.next(),Product.class);
System.out.println(prod.getDescription());
}*/
}catch(Exception e)
{e.printStackTrace();}
finally{
if(home != null){
Iterator i;
System.out.println("destroying products");
try{
i=home.findAllProducts().iterator();
while(i.hasNext())
{
try{
Product prod=(Product)PortableRemoteObject.narrow(i.next(),Product.class);
if(prod.getProductID().startsWith("123"))
{
prod.remove();
}
}catch(Exception e){e.printStackTrace();}
}
}catch(Exception e){}
}
}
System.out.println("Hai");

}
}


what r things i have to add.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Check out the following discussion:
http://www.coderanch.com/t/160156/java-EJB-SCBCD/certification/Remote-client-access



SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Ramesh kumaar
Ranch Hand

Joined: Mar 19, 2002
Posts: 146
Hi,

I belive that you forget to pass the system properties to InitialContext()

Ex:

Properties prop= System.getProperties();

Context ctx= new InitialContext(prop);

better try this.

Regards,
Rameshkumar
naveen babu
Greenhorn

Joined: Nov 10, 2004
Posts: 8
i have change my code as
----------------------------------------------------------------------------
package student;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import java.util.*;

public class StudentClient
{
public static void main(String args[])
{
System.out.println("i am in start");
try{

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");
//env.setProperty("j2ee.clientName","Student");
env.setProperty("java.naming.factory.url.pkgs","org.jboss.naming.clientj2ee.clientName:StudentClient");

InitialContext jndiContext = new InitialContext(env);
StudentHome home=(StudentHome)PortableRemoteObject.narrow(jndiContext.lookup("StudentHome"),StudentHome.class);

home.create("Ammamn","A01");

}catch(Exception e){System.out.println("error in client"+e);}

System.out.println("i am in end");
}

}
with this no InitialContextError

now i have a new pblm.

the client can't bound the Remote Home Interface.

StudentHome can't bound
M Murthy
Ranch Hand

Joined: Nov 20, 2004
Posts: 33
What exactly is the new error?
If the client is not able to access the Home interface then the problem might be because you dont have the ejb jar file in your classpath. Make the jar file accessible to the client by setting it in the classpath.


Sindhu Murthy<br />SCJP 1.4<br />SCBCD
naveen babu
Greenhorn

Joined: Nov 10, 2004
Posts: 8
where is that ejb jar file
i have to create or existing file
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling EJB From the Client