Java ClassNotFoundException (Java ClassNotFoundException)

  • Java ClassNotFoundException occurs when the application tries to load a class but Classloader is not able to find it in the classpath.当应用程序尝试加载类,但Classloader无法在类路径中找到它时,将发生Java ClassNotFoundException。
  • Common causes of java.lang.ClassNotFoundException are using Class.forName or ClassLoader.loadClass to load a class by passing String name of a class and it’s not found on the classpath.java.lang.ClassNotFoundException常见原因是使用Class.forNameClassLoader.loadClass通过传递类的String名称来加载类,而在类路径上找不到它。
  • ClassNotFoundException is a checked exception, so it has to be catch or thrown to the caller.ClassNotFoundException是一个已检查的异常 ,因此必须将其捕获或抛出给调用者。
  • ClassNotFoundException always occurs at runtime because we are indirectly loading the class using Classloader. Java compiler has no way to know if the class will be present in the classpath at runtime or not.ClassNotFoundException总是在运行时发生,因为我们使用Classloader间接加载类。 Java编译器无法知道在运行时该类是否存在于类路径中。
  • One of the most common example of ClassNotFoundException is when we try to load JDBC drivers using Class.forName but forget to add it’s jar file in the classpath.ClassNotFoundException的最常见示例之一是,当我们尝试使用Class.forName加载JDBC驱动程序但忘记将其jar文件添加到类路径中时。

Java ClassNotFoundException示例 (Java ClassNotFoundException Example)

Let’s look at a simple example where we will get ClassNotFoundException.

让我们看一个简单的示例,在该示例中将获得ClassNotFoundException

package com.journaldev.exceptions;public class DataTest {public static void main(String[] args) {try {Class.forName("com.journaldev.MyInvisibleClass");ClassLoader.getSystemClassLoader().loadClass("com.journaldev.MyInvisibleClass");ClassLoader.getPlatformClassLoader().loadClass("com.journaldev.MyInvisibleClass");} catch (ClassNotFoundException e) {e.printStackTrace();}}}

Note that com.journaldev.MyInvisibleClass doesn’t exist, so When we execute above program, we get following exception stack trace.

请注意, com.journaldev.MyInvisibleClass不存在,因此当我们执行上述程序时,将获得以下异常堆栈跟踪。

java.lang.ClassNotFoundException: com.journaldev.MyInvisibleClassat java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)at java.base/java.lang.Class.forName0(Native Method)at java.base/java.lang.Class.forName(Class.java:292)at com.journaldev.exceptions.DataTest.main(DataTest.java:7)

In above example, all the three statements will throw java.lang.ClassNotFoundException.

在上面的示例中,所有这三个语句将抛出java.lang.ClassNotFoundException

如何解决ClassNotFoundException (How to resolve ClassNotFoundException)

It’s very easy to fix ClassNotFoundException because the exception stack trace clearly specifies the class not found. Just check for classpath settings and make sure class it’s present at runtime.

修复ClassNotFoundException非常容易,因为异常堆栈跟踪明确指定了未找到的类。 只需检查类路径设置,并确保其在运行时存在即可。

ClassNotFoundException与NoClassDefFoundError (ClassNotFoundException vs NoClassDefFoundError)

NoClassDefFoundError is a runtime error thrown when a class is not found at runtime. It’s very similar to ClassNotFoundException. Read more at Java NoClassDefFoundError.

NoClassDefFoundError是在运行时找不到类时引发的运行时错误。 它与ClassNotFoundException非常相似。 在Java NoClassDefFoundError上内容。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/20898/java-classnotfoundexception-java-lang-classnotfoundexception

Java ClassNotFoundException – java.lang.ClassNotFoundException相关推荐

  1. java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 解决方法 java.lang.ClassNotFoundException: com.

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 解决方法 java.lang.ClassNotFoundException: com.m ...

  2. Maven项目下update maven后Eclipse报错:java.lang.ClassNotFoundException: org.springfra

    2019独角兽企业重金招聘Python工程师标准>>> 出现这个java.lang.ClassNotFoundException: org.springframework.web.c ...

  3. IDEA里运行代码时出现Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger的解决办法(图文详解)...

    问题详情 运行出现log4j的问题 "C:\Program Files\Java\jdk1.8.0_66\bin\java" -Didea.launcher.port=7533 & ...

  4. java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderL

    转自:https://www.cnblogs.com/qianzf/p/7020613.html java.lang.ClassNotFoundException: org.springframewo ...

  5. java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderL,spring获取context...

    今天学习spring项目的时候出现了下面的错误信息: java.lang.ClassNotFoundException: org.springframework.web.context.Context ...

  6. java.lang.ClassNotFoundException: org.codehaus.jackson.JsonProcessingException 异常解决方案

    在SpringMVC中使用Jackson实现json输出时配置如下: <!-- 输出对象转JSON支持 --> <bean id="stringConverter" ...

  7. jsf项目启动报:java.lang.ClassNotFoundException: javax.faces.webapp.FacesServlet

    java.lang.ClassNotFoundException: javax.faces.webapp.FacesServlet的错误,尝试了很多的很多的方法,也无疾而终. 解决方法: (1)   ...

  8. java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver

    1. 安装:SQL Server 2000 Driver for JDBC Service Pack 3 下载安装JDBC SP3 http://www.microsoft.com/downloads ...

  9. ShareEntryActivity java.lang.ClassNotFoundException | Android类找不到问题

    错误堆栈: Process: com.mci.smagazine, PID: 23265java.lang.RuntimeException: Unable to instantiate activi ...

  10. Struts2环境下Tomcat启动异常:Exception starting filter struts2,报了一个java.lang.ClassNotFoundException

    Struts2环境下Tomcat启动异常:Exception starting filter struts2,报了一个java.lang.ClassNotFoundException 参考文章: (1 ...

最新文章

  1. ic卡复制软件_使用MCT复制IC卡0扇区的方法(适用于NFC手机复制或模拟门禁卡)...
  2. iOS开发CoreAnimation解读之一——初识CoreAnimation核心动画编程
  3. P3348-[ZJOI2016]大森林【LCT】
  4. 数据结构基础(1) --Swap Bubble-Sort Select-Sort
  5. ROS仿真-记一次错误 gazebo-2 process has died exit code 2
  6. 推荐一个强大的Go库-rk-boot,未来 Go领域的SpringBoot?
  7. 技术人解读企业为什么要平台化,关于数据中台你不知道的事...
  8. vue data为什么是函数_由 Vue 中三个常见问题引发的深度思考
  9. TensorFlow函数(四)tf.trainable_variable() 和 tf.all_variable()
  10. 【水果识别】基于matlab GUI形态学水果大小识别【含Matlab源码 920期】
  11. 海康人脸库上传人脸图片和人员扩展信息踩坑记
  12. int与byte[]之间的相互转换
  13. BZOJ2434: [Noi2011]阿狸的打字机
  14. OpenCASCADE:OCCT应用框架OCAF之XML支持
  15. transformer t5 relative position代码解读
  16. 计算机太极之光,3000多名研究生赛太极,五大太极拳流派名家展风采
  17. python3 排列组合
  18. 高仿微信 Windows 端
  19. wps里面如何使用计算机,wps表格中怎么在多行之间每行插一行?
  20. 屁股决定脑袋--是否可以把0603电容更换为0402的电容

热门文章

  1. asp.net中用LinkButton取到gridview中当前行的ID值
  2. [转载] python 语言基础 - 字符串常用函数及操作
  3. Vue.js 学习笔记 八 v-for
  4. IntelliJ IDEA(三、各种工程的创建 -- 之二 -- 创建一个JavaWeb工程)
  5. Java--文档注释
  6. catch(…) vs catch(CException *)?
  7. BZOJ2007 NOI2010 海拔 网络流+最短路
  8. VM安装rhel或linux后,声音很响,如何关闭
  9. Course 2: 单视图计量
  10. CListCtrl,SetItemState 高亮(显示蓝色)