Bootstrap FreeKB - IBM WebSphere - Resolve "ClassNotFoundException"
IBM WebSphere - Resolve "ClassNotFoundException"

Updated:   |  IBM WebSphere articles

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.

java.lang.ClassNotFoundException: com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource

 

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;

 


Application and JAR file

If an application is getting ClassNotFoundException, you will first need to determine how the classes in the JAR file are being made available to the Java application. The classes in a JAR file can be made available to a Java application running in a WebSphere application server in the following ways.

  • The JAR file can be placed in the applications WEB-INF/lib folder
  • The JAR file can be placed in the application servers classpath
  • The JAR file can be placed Shared Library

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.

 


Data Source

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.

com/mysql/cj/jdbc/MysqlDataSource.class

 

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.

  1. In the WebSphere admin console, select Resources > JDBC JDBC Providers.
  2. Select your JDBC provider.
  3. Update the implementation class, and select OK.
  4. Select Save.

 

If there are no other issues with the JDBC and data source, the test connection should now be successful.




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter f52ec2 in the box below so that we can be sure you are a human.