NoInitialContextException

Discussions

XML & Web services: NoInitialContextException

  1. NoInitialContextException (4 messages)

    Please help. I am not able to get over this one.

    Error:
    javax.naming.NoInitialContextException: Need to specify class name in environmen
    t or system property, or as an applet parameter, or in an application resource f
    ile: 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 myeg.main(myeg.java:24)

    Here is my server.xml:

    <Context className="org.apache.catalina.core.StandardContext" cachingAllowed="true" charsetMapperClass="org.apache.catalina.util.CharsetMapper" cookies="true" crossContext="true" debug="0" docBase="d:/tomcat/webapps/axis" mapperClass="org.apache.catalina.core.StandardContextMapper" path="/axis" privileged="false" reloadable="false" swallowOutput="false" useNaming="true" wrapperClass="org.apache.catalina.core.StandardWrapper">
    <Resource auth="Container" name="jdbc/aews" debug="0" type="javax.sql.DataSource"/>
    <ResourceParams name="jdbc/aews">
    <parameter>
    <name>factory</name>
    <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
    </parameter>
    <parameter>
    <name>username</name>
    <value>dnbpmo</value>
    </parameter>
    <parameter>
    <name>url</name>
    <value>jdbc:microsoft:sqlserver://ctsinentcfac:1433</value>
    </parameter>
    <parameter>
    <name>maxActive</name>
    <value>500</value>
    </parameter>
    <parameter>
    <name>driverClassName</name>
    <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
    </parameter>
    <parameter>
    <name>password</name>
    <value>dnbpmo</value>
    </parameter>
    <parameter>
    <name>maxIdle</name>
    <value>200</value>
    </parameter>
    </ResourceParams>
    </Context>

    Client

    Context initCtx = new InitialContext();

    Context envCtx = (Context)initCtx.lookup("java:comp/env");

    DataSource ds = (DataSource)envCtx.lookup("/jdbc/aews");
    Connection conn = ds.getConnection();
  2. If the code that you have put is in a servlet or some class that runs inside the container, then you will have no problem, otherwise u have to get the initial context using a properties object as argument. Typically the properties object will have the provider URL, username, password etc that will be used to get the initial context from a remote client JVM.
  3. thanks! I put the code inside the container. Its working now. But I am getting another exception. I am trying to connect to oracle. But it gives an SQLException. I've put all the class files mentioned in the Readme.txt file of the oracle installation, into their respective environment paths.I have used the same code as that above. Only the driver and url in server.xml have been changed. and of course user and pass.

    java.sql.SQLException: Cannot load JDBC driver class 'oracle.jdbc.OracleDriver()
    '
            at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSou
    rce.java:529)
            at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource
    .java:312)
            at samples.connection.connservice.service(connservice.java:43)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
    java:39)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
    sorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:324)
            at org.apache.axis.providers.java.RPCProvider.invokeMethod(RPCProvider.j
    ava:402)
            at org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider
    .java:309)
            at org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:
    333)
            at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrateg
    y.java:71)
            at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:150)
            at org.apache.axis.SimpleChain.invoke(SimpleChain.java:120)
            at org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:481
    )
            at org.apache.axis.server.AxisServer.invoke(AxisServer.java:323)
            at org.apache.axis.transport.http.AxisServlet.invokeEndpointFromGet(Axis
    Servlet.java:544)
            at org.apache.axis.transport.http.AxisServlet.processMethodRequest(AxisS
    ervlet.java:466)
            at org.apache.axis.transport.http.AxisServlet.doGet(AxisServlet.java:294
    )
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
            at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBas
    e.java:339)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
    icationFilterChain.java:247)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
    ilterChain.java:193)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
    alve.java:260)
            at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContex
    t.invokeNext(StandardPipeline.java:643)
            at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
    a:480)
            at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)

            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
    alve.java:191)
            at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContex
    t.invokeNext(StandardPipeline.java:643)
            at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
    a:480)
            at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)

            at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:
    2415)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
    ava:180)
            at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContex
    t.invokeNext(StandardPipeline.java:643)
            at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatche
    rValve.java:170)
            at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContex
    t.invokeNext(StandardPipeline.java:641)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
    ava:172)
            at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContex
    t.invokeNext(StandardPipeline.java:641)
            at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
    a:480)
            at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)

            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
    ve.java:174)
            at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContex
    t.invokeNext(StandardPipeline.java:643)
            at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
    a:480)
            at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)

            at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:22
    3)
            at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
    :432)
            at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
    ssConnection(Http11Protocol.java:386)
            at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java
    :534)
            at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
    ool.java:530)
            at java.lang.Thread.run(Thread.java:534)
  4. I am not sure why this error is coming. Is the classes111/classes12.zip in the war file? If not, it should be in the lib directory. Otherwise it should be in the server classpath. Best thing to try out is to write a simple java program to connect to the database and try executing a simple select. If that works, u can try the same option in the web service. I have not used the dbcp options very much - as we use weblogic connection pools extensively.
  5. re Inside container /outside??[ Go to top ]

    hi Sowmya,

    I want to write some junit test for classes which runs fine inside a container(tomcat), where I provide the parameters like datbase connection string, username, password etc in my context.xml file.
    Can U tell me how can I do this from a junit test class which doesn't runs inside the container

    Thanks
    Rahul