• Post Reply Bookmark Topic Watch Topic
  • New Topic

NoInitialContextException:

 
Clinton Morrison
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to run the java client but can see what I am doing wrong? I have tried several other posts but it does not resolve the situation. Thank you in advance for your help.

Step 1
javac -cp %CLASSPATH%;AdviceAppClient.jar; AdviceClient.java

Step 2
java -cp %CLASSPATH%;AdviceAppClient.jar; AdviceClient

Error Message
javax.naming.NoInitialContextException: Need to specify class name in environmen
t or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at AdviceClient.go(AdviceClient.java:17)
at AdviceClient.main(AdviceClient.java:10)

My environment variables
%JAVA_HOME%
Environment variable C:\Sun\AppServer\jdk
%J2EE_HOME%
Environment variable C:\Sun\AppServer
%CLASSPATH%
Environment variable C:\Sun\AppServer\lib\j2ee.jar;
%PATH%
Environment variable C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\
Program Files\ATI Technologies\ATI Control Panel;C:\Sun\AppServer\jdk\bin;C:\Pro
gram Files\apache-ant-1.7.0-bin\bin;C:\Sun\AppServer\bin;C:\WINDOWS\system32;C:\
WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\ATI Technologies\ATI Control P
anel;C:\Sun\AppServer\jdk\bin;C:\Program

This is the java class Advice Client
import javax.naming.Context;
import javax.naming.InitialContext;
import java.rmi.*;
import javax.rmi.*;
import headfirst.*;
import javax.ejb.*;

public class AdviceClient {
public static void main(String[] args){
new AdviceClient().go();
}

public void go(){
try{
Context ic = new InitialContext();
Object o = ic.lookup("ejb/Advisor");
AdviceHome home = (AdviceHome)PortableRemoteObject.narrow(o, AdviceHome.class);
Advice advisor = home.create();
System.out.println(advisor.getMessage());

}
catch(Exception ex){
ex.printStackTrace();
}
}
}
 
badri nath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check if you have done every thing right in ejb-jar.xml and weblogic-ejb-jar.xml files.
 
badri nath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also i see you are not providing initial_context_factory and provider_url for the server on which you are running the application
Context ic = new InitialContext();
Object o = ic.lookup("ejb/Advisor");


It should look some thing like this..
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.PROVIDER_URL, "localhost:1099");
Context ctx = new InitialContext(env);

PS: Give correct initial_context_factory and provider_url of the server.
 
Clinton Morrison
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Badri,
Thank you for the response. I am running the application on my local machine using the Sun Java System Application Server PE 8.2. I made some changes to the code but don't really know what I am trying to achieve here? When I tried to run the code I received the error below.

1) How can I retrieve the correct initial_context_factory
2) How can I retrieve the correct provider_url of the server?

Thanks again
Clinton

Error Message
javax.naming.NamingException: Cannot parse url: iiop://localhost:3700 [Root exce
ption is java.net.MalformedURLException: Not an LDAP URL: iiop://localhost:3700]

at com.sun.jndi.ldap.LdapURL.<init>(Unknown Source)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(Unknown Source)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(Unknown Source)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(Unknown Source)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(Unknown Source)
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.init(Unknown Source)
at javax.naming.InitialContext.<init>(Unknown Source)
at AdviceClient.go(AdviceClient.java:20)
at AdviceClient.main(AdviceClient.java:12)
Caused by: java.net.MalformedURLException: Not an LDAP URL: iiop://localhost:3700
Class
import javax.naming.Context;
import javax.naming.InitialContext;
import java.rmi.*;
import java.util.Hashtable;

import javax.rmi.*;
import headfirst.*;
import javax.ejb.*;

public class AdviceClient {
public static void main(String[] args){
new AdviceClient().go();
}

public void go(){
try{
Hashtable<Object, Object> env = new Hashtable<Object, Object>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "iiop://localhost:3700");
Context ctx = new InitialContext(env);


Object o = ctx.lookup("ejb/Advisor");

AdviceHome home = (AdviceHome)PortableRemoteObject.narrow(o, AdviceHome.class);
Advice advisor = home.create();
System.out.println(advisor.getMessage());

}
catch(Exception ex){
ex.printStackTrace();
}
}
}
[ November 12, 2007: Message edited by: Clinton Morrison ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!