本文介绍了未处理的异常编译错误:ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个常见的问题,但是尽管在IntelliJ IDEA中将 mysql-connector 添加到我的库中,IDE仍然无法找到该类。正如您在下面的屏幕截图中看到的,我已经将 mysql-connector jar添加为我的项目的库,但它似乎没有看到它存在:

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:

除了'将库添加到项目'之外,我还没有找到解决方案。好像在某个地方有一个缺失的步骤...

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...

推荐答案

您还需要将库添加为依赖项需要它的模块。

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

选择项目设置>模块。选择需要库的模块(在您的情况下,您的项目中似乎只有一个模块, ChatBot )。选择依赖关系选项卡。单击+按钮并选择 Library ... )。最后,选择您添加到项目中的 mysql-connector .. 库。

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.

编辑:我现在看到这根本不是你的问题。您的代码的问题是您从 Class.forName()中有一个未处理的异常。该方法可以抛出已检查的异常: ClassNotFoundException ,必须通过添加 catch 或添加<$ c来处理$ c>将ClassNotFoundException 抛出到 getConnection()的方法签名。

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这样的情况下代码中有错误,最简单的方法就是找出错误的方法,只需将插入符号移动到带有红色边框线的代码中,然后查看底部状态栏中IDEA所说的内容。或者,您可以将鼠标指针悬停在其上方,并将错误消息显示为弹出窗口。

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.

这篇关于未处理的异常编译错误:ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 09:08