Dylan Dylan -4 years ago 444
Java Question

Unhandled exception compile error: ClassNotFoundException

I know this is a common question, but despite adding

mysql-connector
to my library in IntelliJ IDEA, the IDE still can't seem to locate the class. As you can see in the screenshots below, I have added the
mysql-connector
jar as a library for my project, yet it doesn't seem to see that it is present:

enter image description here

enter image description here

enter image description here

I haven't been able to find a solution other than 'add the library to the project'. It seems as though there is a missing step somewhere...

Answer Source

You will also need to add the library as a dependency to the module that needs it.

Choose Project Settings > Modules. Select the Module that needs the library (in your case it seems like you have only one module in your project, ChatBot). Select the Dependencies tab. Click the '+' button and choose Library...). Finally, select the mysql-connector.. library that you added to the project.

Edit: I see now that this wasn't your problem at all. The problem with your code is that you have an unhandled exception from Class.forName(). The method can throw the checked exception: ClassNotFoundException, which must be handled by adding a catch or by adding throws ClassNotFoundException to the method signature of getConnection().

In such cases with error in the code, the easiest way to figure out what's wrong to simply move the caret to the code with the red squigly line and see what IDEA says in the bottom status bar. Alternatively you can hover the mouse pointer above it and the error message is presented as a popup.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download