Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling EJB From the Client

 
naveen babu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the following discussion:
http://www.coderanch.com/t/160156/java-EJB-SCBCD/certification/Remote-client-access

 
Ramesh kumaar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
naveen babu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where is that ejb jar file
i have to create or existing file
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic