• Post Reply Bookmark Topic Watch Topic
  • New Topic

NoInitialContextException

 
ved gunjan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! All
I'm programing JNDI client for the first time.Below i've given my JNDI client code.I'm using JSDK1.4.The code is compiling properly but it is giving me the following error

Problem looking up : javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.jndi.fscontext.RefFSContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.jndi.fscontext.RefFSContextFactory]


Here's the JNDI client code

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.lang.String;
import java.io.*;
import java.util.Hashtable;
public class Lookup
{

public static void main(String[] args)//throws FileNotFoundException
{
File file;
file=new File("C:\\work_dir\\helloworld.txt");
System.out .println("filename" +" "+ file.getName());
System.out.println(file);
try
{
if(file.exists())//throw new FileNotFoundException();
System.out.println("file found");
else
System.out.println("file not present");
}
catch(Exception fe)
{
System.out.println(fe);
}
// Set up environment for creating the initial context
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.fscontext.RefFSContextFactory");
env.put(Context.PROVIDER_URL,"file:c:\\work_dir\\");

try {
// Create the initial context
Context ctx = new InitialContext(env);

//bind the object
ctx.bind("my_file",file);
// Look up an object
File fileobj = (File)ctx.lookup("my_file");
System.out.println(fileobj);
//close the context
ctx.close();
}
catch (NamingException e)
{
System.err.println("Problem looking up " + ": " + e);
}
}

Can any body help my out in rectifying the fault.Any help will be highly appreciated.
Many Regards
Ved gunjan
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which container are you using? See the documentation of the container for how to get the Context?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!