我无法让它工作!
我在 stackoverflow 上发现了很多这样的问题,但 90% 的问题与缺少包名称有关,但我没有任何包,它是“未命名的”,因此不能在 forName()
中定义。参数。
A class that is not in a named package is in an unnamed package. Thus the full class name is "Main". Such classes cannot be used from a named package, except via reflection.
另一个解决方案是指定类路径,但是 src (蓝色文件夹)是一个类路径,如此处所述
You can right click on any directory in your IntelliJ project, select "Mark Directory As...", and choose "Source Root". That director folder will change color from yellow to blue; IntelliJ considers all those directories to be in your CLASSPATH.
但是还是不行!它慢慢地让我发疯。
最佳答案
该消息意味着您需要处理已检查的异常,即 java.lang.ClassNotFoundException
。这并不意味着您无法找到/加载此类。
为了处理检查异常,您可以
- 将代码用
try {...} catch (ClassNotFoundException ex) {...}
括起来. - 将其添加到
main
的抛出声明中方法:main(String[] args) throws ClassNotFoundException
.
在 IDEA 中,您始终可以使用 alt + enter 查看建议。
关于java - Class.forName() - JDK7 - ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20038584/