aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes How can we use JNDI? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How can we use JNDI? " Watch "How can we use JNDI? " New topic
Author

How can we use JNDI?

rameshmca rk
Greenhorn

Joined: Jan 27, 2007
Posts: 17
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

Joined: Feb 05, 2001
Posts: 17249
    
    6

Can you post your error?

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
rameshmca rk
Greenhorn

Joined: Jan 27, 2007
Posts: 17
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

Joined: Feb 01, 2007
Posts: 59
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

Joined: Mar 11, 2007
Posts: 4
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

Joined: Feb 01, 2007
Posts: 59
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can we use JNDI?
 
Similar Threads
JNDI - Datasource Error in Weblogic
problems with JNDI name lookup
JNDI: java:comp/env bound and unbound at the same time?
Dependency INjection through Annotations
Bean Not Bound Exception