Search code examples
jspwarwildflyear

WildFly Error while running application --java.lang.ClassNotFoundException: javax.naming.InitialContext


I'm using JSPs and WildFly 9.0.2.Final with the following conditions:

  1. A JSP file (test.jsp) is accessing Test.java file.

  2. Test.java file is part of testclient.jar and has been deployed as a module.

  3. test.jsp file is part of Test.ear and has been deployed successfully but following error occurred while accessing the Test.java file from test.jsp:

    09:50:35,036 ERROR [io.undertow.request] (default task-1) UT005023: Exception handling request to /test/test.jsp: javax.servlet.ServletException: java.lang.NoClassDefFoundError: javax/naming/InitialContext
    at org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)
    at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:777)
    at org.apache.jsp.test_jsp._jspService(test_jsp.java:70)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:69)
    Caused by: java.lang.NoClassDefFoundError: javax/naming/InitialContext
    at com.testmodule.pojo.Test.getDbConnection(Test.java:12)
    at org.apache.jsp.test_jsp._jspService(test_jsp.java:58)
    ... 34 more
    Caused by: java.lang.ClassNotFoundException: javax.naming.InitialContext from [Module "testclient:main" from local module loader @707f7052 (finder: local module finder @11028347 (roots: /wildfly-9.0.2.Final/modules,/wildfly-9.0.2.Final/modules/system/layers/base))]
    at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:205)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:455)
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:404)
    

These are the contents of test.jsp file present in test.ear:

<%@page import="com.testmodule.pojo.Test"%>
<% try {
    System.out.println(" Going to call getDbConnection method of 'Test' class. This 'Test' class shall be "
        +" part of testclient.jar. testclient.jar shall be deployed as a module --- module add --name=testclient --resources=~/Downloads/lib/test/testclient.jar");
    System.out.println("This file test.jsp shall be present in test.ear...'jboss-deployment-structure.xml' shall be present in "
        +" in META-INF directory of test.ear with entry as -- <dependencies><module name=\"testclient\" export=\"true\" /> </dependencies>");
    new Test().getDbConnection();
} catch(Exception e){
    e.printStackTrace();
} %>

And these are the contents of Test.java present in testclient.jar:

package com.testmodule.pojo;

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;

public class Test { 
    public void getDbConnection(){
        try{
            Context context = new InitialContext(new Properties());
        } catch(Exception e){}
    }
}

Solution

  • I think you need to add a dependency to the module: javax.api.

    Try to add

    <dependencies>
        <module name="javax.api"/>
    </dependencies>
    

    to your module.xml