> Java > java지도 시간 > 본문

자바 클래스NotFoundException

WBOY
풀어 주다: 2024-08-30 16:14:44
원래의
793명이 탐색했습니다.

Java에서는 이름 그대로 JVM(Java Virtual Machine)에서 특정 클래스를 로드하려고 할 때 ClassNotFoundException이 발생합니다. 요청한 클래스를 귀하가 지정한 클래스의 경로에서 찾을 수 없습니다. 이는 귀하가 지정한 클래스의 경로가 손상되었음을 의미하며, 이는 Java 세계에서 실제로 흔히 발생하는 문제입니다. 따라서 ClassNotFoundException은 Java에서도 일반적입니다. 이 문제는 Java 초보자에게 매우 혼란스러운 문제이며, ClassNotFoundException을 잡아내거나 호출자에게 던져야 합니다. ClassNotFoundException은 확인된 예외입니다.

광고 이 카테고리에서 인기 있는 강좌 JAVA MASTERY - 전문 분야 | 78 코스 시리즈 | 15가지 모의고사

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

Java의 ClassNotFoundException 구문은 다음과 같습니다. 

java.lang.ClassNotFoundException:Class_name at location
로그인 후 복사

Java에서 ClassNotFoundException 작업

  • 애플리케이션이 클래스를 로드하려고 시도하는 동안 클래스 로더가 지정된 클래스의 경로에서 해당 클래스를 찾을 수 없는 경우, 즉 Java에서 ClassNotFoundException이 발생하는 경우입니다.
  • 지정된 클래스의 경로에 없는 매개변수로 클래스의 문자열 이름을 전달하면서 클래스를 로드하기 위해 Class.forName 및 ClassLoader.loadClass를 사용하는 것은 java.lang이 발생하는 일반적인 원인 중 하나입니다. Java의 .ClassNotFoundException.
  • Java의 ClassNotFoundException은 확인된 예외이므로 catch하거나 호출자에게 던져야 합니다.
  • Classloader를 사용하여 클래스를 간접적으로 로딩하고 있습니다. 결과적으로 Java에서는 런타임 시 ClassNotFoundException이 발생합니다. 지정된 클래스의 경로에 클래스가 있는지 런타임에 Java 컴파일러가 알 수 있는 방법은 없습니다.
  • Class.forName을 사용하여 JDBC 드라이버를 로드하려고 시도하고 클래스 경로에 jar 파일을 추가하지 않는 것은 Java에서 ClassNotFoundException이 발생하는 일반적인 예 중 하나입니다.
  • Java에서 ClassNotFoundException을 시연하려면 아래 프로그램을 고려하세요.
//a class called program is defined
public class Program
{
//main method is called
public static void main(String args[])
{
//class not found exception is defined using try and catch block
try
{
// the forname method in class class looks for the mentioned class
Class.forName("The Class do not Exist");
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
로그인 후 복사

위 프로그램의 출력은 아래 스냅샷과 같습니다.

자바 클래스NotFoundException

위 프로그램에는 Program이라는 클래스가 정의되어 있습니다. 그런 다음 기본 메서드가 호출됩니다. 그런 다음 클래스를 찾을 수 없음 예외는 try 및 catch 블록을 사용하여 정의됩니다. 클래스가 존재하지 않습니다. 클래스 로더가 찾으려고 하는 java 클래스와 클래스의 forname 메소드가 언급된 클래스를 찾지만 찾지 못합니다. 따라서 ClassNotFoundException이 발생합니다. 프로그램의 출력은 위의 스냅샷과 같습니다.

건축자

Java에는 ClassNotFoundException에 대한 여러 생성자가 있습니다. 그들은:

  • ClassNotFoundException(): 스택의 현재 추적을 포함하는 새로운 ClassNotFoundException이 생성됩니다.
  • ClassNotFoundException(String): 스택의 현재 추적과 지정된 메시지를 포함하는 새로운 ClassNotFoundException이 생성됩니다.
  • ClassNotFoundException(IntPtr, JniHandleOwnership): JNI 객체를 생성하는 동안 표현을 관리하는 동안 이 생성자가 사용되며 런타임에서 이를 호출합니다.
  • ClassNotFoundException(String, Throwable): 스택의 현재 추적과 지정된 메시지의 세부정보 및 클래스를 로드하려고 할 때 발생하는 예외를 포함하는 새로운 ClassNotFoundException이 생성됩니다. .

Java ClassNotFoundException의 예

아래에 언급된 예는 다음과 같습니다.

예시 #1

ClassNotFoundException을 시연하는 Java 프로그램:

 코드:

//a class called exceptiondemo is defined
public class Exceptiondemo
{
//a string variable is defined
private static final String DRIVE_CLASS = "com.mysql.jdbc.Driver";
//main method is called including the exception
public static void main(String[]  args) throws Exception
{
System.out.println("MySQL JDBC driver loading attempt");
//the forname method in class class looks for the mentioned class
Class.forName(DRIVE_CLASS);
}
}
로그인 후 복사

위 프로그램의 출력은 아래 스냅샷과 같습니다.

자바 클래스NotFoundException

위 프로그램에는 Exception 데모라는 클래스가 정의되어 있습니다. 그런 다음 기본 메서드가 호출됩니다. 그런 다음 JDBC 드라이버 경로가 할당되는 문자열 변수가 정의됩니다. 그런 다음 예외를 발생시키는 기본 메서드가 호출됩니다. 클래스 로더는 지정된 클래스의 JDBC 드라이버 경로를 찾으려고 시도하고 클래스의 forname 메소드는 언급된 클래스를 찾지만 실패합니다. 따라서 ClassNotFoundException이 발생합니다. 프로그램의 출력은 위의 스냅샷과 같습니다.

예시 #2

ClassNotFoundException(String)을 시연하는 Java 프로그램

코드:

//a class called check is defined
public class Check
{
//main method is called
public static void main(String args[])
{
//class not found exception is defined using try catch block
try
{
//the forname method in class class looks for the mentioned class
Class.forName("Demonstrating class not found exception");
}
catch(ClassNotFoundException e)
{
//the string specified along with the class not found exception is displayed.
System.out.println("There is no class as specified in the path " + e);
}
}
}
로그인 후 복사

위 프로그램의 출력은 아래 스냅샷과 같습니다.

자바 클래스NotFoundException

In the above program, a class called check is defined. Then the main method is called. Then the main method is called. Then class not found exception is defined by using try and catch block.  Then the forename method in class looks for the mentioned class, which it fails to find; hence the ClassNotFoundException is thrown and the string specified as a detailed message along with the class not found exception is displayed. The output of the program is as shown in the snapshot above.

How to Avoid ClassNotFoundException?

Steps to avoid ClassNotFoundException:

  • The file of the class causing the problem on which the jar file is present must be found out.
  • We should check if the path of the class or classpath consists of that jar. If the jar is not present in the classpath or path of the class, the class must be added in the class or classpath path.
  • If the class is present in the class or classpath path, then the chances are that there is an overriding of classpath or path of the class or the path specified in the jar file or the script used for starting up is being used by the application. To solve this problem, we need to find the exact path of the class that is being used by the application.

Conclusion

In this tutorial, we understand the concept of Class Not Found Exception in Java through definition, the syntax of Class Not Found Exception in Java, working of Class Not Found Exception in Java, and their constructors through examples and their outputs.

위 내용은 자바 클래스NotFoundException의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!