Hi,
I wrote this code as is from the JNDI tutorial from sun website. But I am getting this exception while running it:

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.*;

/**
 * Write a description of class FirstJndi here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class FirstJndi
{
    public static void main(String[] args) {
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFatory");
        try {
            Context ctx = new InitialContext(env);
        
            Object obj = ctx.lookup(args[0]);
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
}

Can anyone help me with this? I would really appreciate this very much. Thanks in advance.

Ensure that the required JARs are on the runtime classpath. You can download the required JNDI JARs from here.

Also, paste the entire stacktrace along with the tutorial you are referring to for others reference.

Thanks for your response. I really appreciate this. Thanks for the link you gave. I followed it and downloaded the jndi1.2.1 library. And as can be seen below I included them in the class path as well. But unfortunately it is still not working. I was hoping that it would work.

C:\Users\johndoe444\Documents\java>dir
 Volume in drive C is OSDisk
 Volume Serial Number is D8AD-951F

 Directory of C:\Users\johndoe444\Documents\java

06/07/2010  02:25 PM    <DIR>          .
06/07/2010  02:25 PM    <DIR>          ..
06/07/2010  02:19 PM               455 bluej.pkg
06/07/2010  02:27 PM               818 FirstJndi.class
06/07/2010  11:55 AM                97 FirstJndi.ctxt
06/07/2010  11:55 AM               688 FirstJndi.java
06/27/2000  03:22 AM            22,769 fscontext.jar
06/07/2010  02:19 PM               455 package.bluej
06/27/2000  03:21 AM            77,116 providerutil.jar
06/07/2010  11:46 AM               483 README.TXT
               8 File(s)        102,881 bytes
               2 Dir(s)  149,327,269,888 bytes free

C:\Users\johndoe444\Documents\java>javac -cp .;fscontext.jar;providerutil.jar Firs
tJndi.java
Note: FirstJndi.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

C:\Users\johndoe444\Documents\java>java -cp .;fscontext.jar;providerutil.jar First
Jndi
javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.jndi.f
scontext.RefFSContextFatory [Root exception is java.lang.ClassNotFoundException:
 com.sun.jndi.fscontext.RefFSContextFatory]
        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 FirstJndi.main(FirstJndi.java:18)
Caused by: java.lang.ClassNotFoundException: com.sun.jndi.fscontext.RefFSContext
Fatory
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)
        ... 5 more

The jndi tutorial can be achieved directly from sun website.
http://java.sun.com/products/jndi/tutorial/information/download.html

It is fscontext.jar which is required to be setup on classpath to fix this issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.