Home > Blockchain >  ClassNotFoundException: org.apache.catalina.realm.JDBCRealm
ClassNotFoundException: org.apache.catalina.realm.JDBCRealm

Time:08-24

I am trying To implement security for my application, so i added a Realm element to the application's context.xml file as following

<Context path="/myApp_admin2">
    <Realm className="org.apache.catalina.realm.JDBCRealm" 
           driverClassName="com.mysql.cj.jdbc.Driver"
           onnectionURL="jdbc:mysql://localhost:3306/mydb?autoReconnect=true" 
           connectionName="root" connectionPassword="12345" 
           debug="99"
           roleNameCol="Rolename" userCredCol="Password" 
           userNameCol="Username" userRoleTable="userrole" userTable="userpass"/>
</Context>

when I run the application I received class not found Exception

java.lang.ClassNotFoundException: org.apache.catalina.realm.JDBCRealm
        at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:433)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:586)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
        at org.apache.tomcat.util.digester.ObjectCreateRule.begin(ObjectCreateRule.java:102)
        at org.apache.tomcat.util.digester.Digester.startElement(Digester.java:1264)
        at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:518)
        at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:183)
        at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1386)
        at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2725)
        at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:605)
        at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:541)
        at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:888)
        at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:824)
        at java.xml/com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
        at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1224)
        at java.xml/com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:635)
        at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1522)
        at org.apache.catalina.startup.ContextConfig.processContextConfig(ContextConfig.java:735)
        at org.apache.catalina.startup.ContextConfig.contextConfig(ContextConfig.java:679)
        at org.apache.catalina.startup.ContextConfig.init(ContextConfig.java:949)
        at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:314)
        at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
        at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:423)
        at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:137)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:173)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:726)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:698)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:747)
        at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:693)
        at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1979)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
        at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:123)
        at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:586)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:476)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1708)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:320)
        at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
        at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:423)
        at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:366)
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:946)
        at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:886)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1396)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1386)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
        at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:145)
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:919)
        at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:263)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.StandardService.startInternal(StandardService.java:432)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:927)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:795)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:567)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:345)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:476)
21-Aug-2022 13:04:15.284 SEVERE [main] org.apache.catalina.startup.ContextConfig.processContextConfig Parse error in context.xml for [/myApp_admin2]
    org.xml.sax.SAXParseException; systemId: file:/C:/apache-tomcat-10.0.14/conf/Catalina/localhost/myApp_admin2.xml; lineNumber: 3; columnNumber: 353; Error at line [3] column [353]: [org.apache.catalina.realm.JDBCRealm]
        at org.apache.tomcat.util.digester.Digester.createSAXException(Digester.java:1953)
        at org.apache.tomcat.util.digester.Digester.createSAXException(Digester.java:1985)
        at org.apache.tomcat.util.digester.Digester.startElement(Digester.java:1267)
        at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:518)
        at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:183)
        at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1386)
        at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2725)
        at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:605)
        at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:541)
        at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:888)
        at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:824)
        at java.xml/com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
        at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1224)
        at java.xml/com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:635)
        at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1522)
        at org.apache.catalina.startup.ContextConfig.processContextConfig(ContextConfig.java:735)
        at org.apache.catalina.startup.ContextConfig.contextConfig(ContextConfig.java:679)
        at org.apache.catalina.startup.ContextConfig.init(ContextConfig.java:949)
        at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:314)
        at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
        at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:423)
        at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:137)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:173)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:726)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:698)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:747)
        at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:693)
        at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1979)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
        at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:123)
        at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:586)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:476)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1708)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:320)
        at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
        at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:423)
        at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:366)
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:946)
        at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:886)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1396)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1386)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
        at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:145)
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:919)
        at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:263)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.StandardService.startInternal(StandardService.java:432)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:927)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:795)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:567)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:345)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:476)
    Caused by: java.lang.ClassNotFoundException: org.apache.catalina.realm.JDBCRealm
        at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:433)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:586)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
        at org.apache.tomcat.util.digester.ObjectCreateRule.begin(ObjectCreateRule.java:102)
        at org.apache.tomcat.util.digester.Digester.startElement(Digester.java:1264)
        ... 58 more

I followed the instructions that I got from this website To set up Tomcat to use JDBCRealm, enter image description here 2- the JDBC Driver is added under TOMCAT LIB Folder enter image description here 3- two tables with certain columns are created in the DB

create table userpass (
  username       varchar(15) not null primary key,
  password         varchar(15) not null
);

create table userrole (
  username         varchar(15) not null,
  rolename         varchar(15) not null,
  primary key (username, rolename)
);

So what are your advice and the reason behind such an Error?

Note: I'm using apache-tomcat-10.0.14 Server NetBeans IDE13 JDK 16

CodePudding user response:

The org.apache.catalina.realm.JDBCRealm has been removed in Tomcat 10 onwards. You can use org.apache.catalina.realm.DataSourceRealm instead.

CodePudding user response:

JDBCRealm has been removed in Tomcat 10. From the release notes for Tomcat 9:

The JDBC Database Realm has been deprecated and will be removed in Tomcat 10 onwards. Use the DataSourceRealm instead.

You can verify this by reviewing the javadoc for Tomcat 10.0.23: JDBCRealm doesn't exist.

So either (temporarily) downgrade to Tomcat 9, or switch to using DataSourceRealm.

This old Apache thread Deprecated JDBCRealm may explain the motivation for the removal:

I'd like to proposed the following:
- Deprecated the JDBCRealm in 7.0.x, 8.5.x and 9.0.x
- Remove the JDBCRealm in 10.0.x

The reasons for this are:
- The JDBCRealm is single threaded
- The DataSourceRealm is a better solution
  • Related