ClassNotFoundException occurs, as you may have guessed, when a class cannot be found. For example, something like this may be returned. In this example, class "com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource" is not found.
A number of different actions may return ClassNotFoundException, such as when an app deployed to WebSphere attempts to use a class that is not found, or when a data source attempts to get a class that is not found, or when the wsdamin script attempts to find a class that is not found.
Search for the class
On a Linux system, the following command can be used to find all of the files ending in .jar below the /opt/WebSphere directory, and to then use the Java jar command to check each JAR file for the class that is not found, and to print the results to files.txt.
files=$(find /opt/WebSphere | grep -i [A-Za-z0-9].jar$); for file in $files; do echo $file | tee -a files.txt; jar -tf $file | grep -i 'your class name here' | tee -a files.txt; done;
If an application is getting ClassNotFoundException, you will first need to determine if the application is getting the class from:
Then, ensure the class exists in the class folder or JAR file. If the class exists, use the class loader viewer to ensure the class is being loaded.
If the class expects a certain version of Java, such as Java 8, but the application server SDK is using Java 7, this error may occur.
This error appears when attempting to test the connection to a data source.
As the error suggests, the logs will also record this exception. In the example, the node agent SystemOut.log has the following.
DSRA0174W: Warning: GenericDataSourceHelper is being used. DSRA8040I: Failed to connect to the DataSource "". Encounted java.lang.ClassNotFoundException: com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource
This error suggests some problem with the implementation class being used. In this example, the implementation class being used is com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource. The implementation class is obtained from the JAR file being used to connect to the database. Viewing the content of the JAR file can be used to determine the appropriate implementation class. The content of the JAR file will simply be a collection of files and directories. As an example, following is one of the files and directories in the MySQL JAR.
From this we can conclude that the valid implementation class is actually com.mysql.cj.jdbc.MysqlConnectionPoolDataSource. Once you know the valid implementation class, the JDBC provider in WebSphere can be updated to have the valid implementation class.
If there are no other issues with the JDBC and data source, the test connection should now be successful.