javax.naming.NoInitialContextException
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Brett_McGoldrick
Posted On:   Tuesday, February 26, 2002 01:14 PM

I have just created a datasource and am trying to use it with the following code Context ctx = new InitialContext(); System.out.println("hello1"); DataSource ds = (DataSource)ctx.lookup("jdbc/BrettDataBase"); Once I get the hello1 message I then get an error: 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 Does anyone know what I am doing wrong? Thanks    More>>

I have just created a datasource and am trying to use it with the following code

Context ctx = new InitialContext();



System.out.println("hello1");


DataSource ds = (DataSource)ctx.lookup("jdbc/BrettDataBase");


Once I get the hello1 message I then get an error:


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

Does anyone know what I am doing wrong?

Thanks

   <<Less

Re: javax.naming.NoInitialContextException

Posted By:   Rich_Carpenter  
Posted On:   Friday, March 1, 2002 05:22 AM

Brett,
The class name to use in VAJ or Websphere is as noted
in this simple example that can be used to lookup a data source
locally, such as when running in a servlet in WebSphere:


Hashtable parms = new Hashtable();
parms.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.ejs.ns.jndi.CNInitialContextFactory");
Context ctx = new InitialContext(parms);
ds = (DataSource)ctx.lookup(source);


When looking up from a remote machine, it is necessary to specify
aditional information to be able to locate the name server.
This is a somewhat more verbose example you could work
from that provides additional information when forming
the naming context:


public javax.sql.DataSource getDataSource() {
DataSource ds=null;

if (roleDataSource != null) {
return(roleDataSource);
}

String source=props.getProperty("AF.DataSource");

// Get naming related lookup values
String ip=props.getProperty("AF.DSNamingHost","localhost");
String port=props.getProperty("AF.DSNamingPort","900");
String protocol=props.getProperty("AF.DSNamingProtocol","iiop");
String namingClass=props.getProperty("AF.DSNamingClass",
"com.ibm.ejs.ns.jndi.CNInitialContextFactory");
String namingUserid=props.getProperty("AF.DSNamingUserid");
String namingPassword=props.getProperty("AF.DSNamingPassword");
String namingAuthentication=props.getProperty("AF.DSNamingAuthentication","simple");
String JNDIServer=protocol+"://"+ip+":"+port;

// Create the initial naming context.
Hashtable parms = new Hashtable();
parms.put(Context.INITIAL_CONTEXT_FACTORY, namingClass);
parms.put(Context.PROVIDER_URL, JNDIServer);

// If id/password supplied, add those to naming context as well
if ( (namingUserid != null) && (namingPassword != null) ) {
parms.put(Context.SECURITY_AUTHENTICATION,namingAuthentication);
parms.put(Context.SECURITY_PRINCIPAL,namingUserid);
parms.put(Context.SECURITY_CREDENTIALS,namingPassword);
}

try {
Context ctx = new InitialContext(parms);

// Perform lookup
ds = (DataSource)ctx.lookup("jdbc/" +source);
roleDataSource=ds;
return ds;
}
catch(javax.naming.NamingException e) {
System.out.println("Failure obtaining datasource: " + e.getMessage());
return null;
}

}


Hope this helps,


Rich

Re: javax.naming.NoInitialContextException

Posted By:   Nicholas_Whitehead  
Posted On:   Wednesday, February 27, 2002 08:47 AM

Brett;

You need to supply the JNDI parameters required to make a remote context. So the code you supplied will not work unless it is running in the same VM as your application server. (An EJB for example). If this is the case, I am not sure why you are getting that error. Perhaps you can get back to us with more sample code and the application server version.

Making a remote connection requires that you either:


  • Programatically specifiy the parameters.
  • Enumerate the parameters in a jndi.properties file in your classpath.
  • Make other provisions to ensure that the correct parameters are set in the system properties.


The following is an example the first using WebLogic 6.1:



Properties p = new Properties();

p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");

p.put(Context.PROVIDER_URL, "t3://localhost:7001");

p.put(Context.SECURITY_PRINCIPAL, "system");

p.put(Context.SECURITY_CREDENTIALS, "mypassword");

ctx = new InitialContext(p);



Hope this helps.

About | Sitemap | Contact