1

Iam trying to connect mysql by using JNDI. But it show an exception

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:

See my code :

    VendorDataSource vds = new VendorDataSource();
    vds.setServerName("localhost");
    vds.setDatabaseName("jnditest");
    vds.setDescription("The data source for inventory and personnel");

    try {
            ctx = new InitialContext();
        ctx.bind("jdbc/myds", vds);

    } catch (NamingException e1) {

        e1.printStackTrace();
    }

    try {
        ctx = new InitialContext();
        DataSource ds = (DataSource)ctx.lookup("jdbc/myds");
        Connection conn = ds.getConnection("root", "password");

    } catch (NamingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

And the error message :

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(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.bind(Unknown Source)
    at samplemariadb.DataSourceConnectivity.main(DataSourceConnectivity.java:33)
Haseena
  • 484
  • 2
  • 11
  • 25

1 Answers1

2

usually you need to specify where to get the InitialContext from (and where is located the jndi tree you will use)

You can do it in two ways :

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, 
    "<initialContextFactory>");
env.put(Context.PROVIDER_URL, "<url>");
env.put(Context.SECURITY_PRINCIPAL, "<user>");
env.put(Context.SECURITY_CREDENTIALS, "<password>");
ctx = new InitialContext(env);

or create a jndi.properties and place it in your classpath :

java.naming.factory.initial=<initialContextFactory>
java.naming.provider.url=<url>
java.naming.security.principal=<user>
java.naming.security.credentials=<password>

In your case it is complaining because the lack of INITIAL_CONTEXT_FACTORY (which depends on which Java EE server you are using).

Hope it helps

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
richardtz
  • 4,993
  • 2
  • 27
  • 38
  • Iam trying this method also. But not able to download the fscontext.jar file from no site. Can you help me to give the proper path for downloading the fscontext.jar file? – Haseena Nov 07 '12 at 10:53
  • As far as I know it was included in the Java Web Services Developer Pack, (http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-jwsdp-419428.html) but I am not sure if that's what you need. – richardtz Nov 07 '12 at 11:01
  • @Haseena What do you want that for? What you want is the provider used by your container. – user207421 Mar 17 '19 at 07:33