Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can we use JNDI?

 
rameshmca rk
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to know details about setting names for objects using JNDI naming servcices and also the details of making lookup for those names by using main methos class.I got error while I try this using main method class?Is not possible to make a lookup inside main method class?I require the settings and complete look up program.

Thanks and regards,
Ramesh RK
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post your error?

Mark
 
rameshmca rk
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This is my complete program interfaces bean class and a main method client


HelloBean.java -The bean implementation class

package advice;

import javax.ejb.*;

public class HelloBean implements SessionBean, HelloLocalBusiness {
private SessionContext context;

public void setSessionContext(SessionContext aContext) {
context = aContext;
}

public void ejbActivate() {

}

public void ejbPassivate() {

}

public void ejbRemove() {

}
public void ejbCreate() {
// TODO implement ejbCreate if necessary, acquire resources
// This method has access to the JNDI context so resource aquisition
// spanning all methods can be performed here such as home interfaces
// and data sources.
}

public String sayHello() {
return("Hello");
}

}

HelloLocal.java-Component interface


package advice;

import javax.ejb.EJBLocalObject;


/**
* This is the local interface for Hello enterprise bean.
*/
public interface HelloLocal extends EJBLocalObject, HelloLocalBusiness {
}


HelloLocalHome.java


package advice;

import javax.ejb.CreateException;
import javax.ejb.EJBLocalHome;


/**
* This is the local-home interface for Hello enterprise bean.
*/
public interface HelloLocalHome extends EJBLocalHome
{
HelloLocal create() throws CreateException;
}


HelloLocalBusiness.java


package advice;


/**
* This is the business interface for Hello enterprise bean.
*/
public interface HelloLocalBusiness {
String sayHello();
}

HelloClient.java

package client;
import advice.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import javax.ejb.*;
import java.util.*;

public class HelloClient {

/** Creates a new instance of HelloClient */
public HelloClient() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try
{
Properties prop=System.getProperties();
prop.put("INITIAL_CONTEXT_FACTORY","java.naming.factory.initial");
Context ctx=new InitialContext(prop);
Object o=ctx.lookup("ejb.Hello");
HelloLocalHome helloHome=(HelloLocalHome)o;
HelloLocal hello=helloHome.create();
System.out.println(hello.sayHello());
}
catch(Exception ex)
{
ex.printStackTrace();
}

}

}

output for this client program is follows

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:640)

at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243)

at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:280)

at javax.naming.InitialContext.lookup(InitialContext.java:347)

at client.HelloClient.main(HelloClient.java:37)

I have compiled this program numerous times but got the same result.I used netbeans5.0 IDE and Sun Java Apps Server 8.2. Help me to give the detailed code with descriptions of settings for env entries and URL Specifications to access the local host.

Thanks and regards,
Ramesh RK
 
Joan Horta Tosas
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line is incorrect:

INITIAL_CONTEXT_FACTORY is a static string which content is java.naming.factory.initial. You have to either put:

or

where contextFactoryClassName is the server context factory class... For Sun's, check this https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#nonJavaEEwebcontainerRemoteEJB
 
Kamal hasan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joan,
thanks for suggestion.but i did not get the proper class for context factory.Could you give me the fully qualified class name for the property?
Have to I give any host and port specification while running the client?

Thanks and regards,
Kamal hasan
 
Joan Horta Tosas
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the URL at the end of my last post if you're connecting to a Sun's Server. Otherwise, check your server documentation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic