Skip to main content

ClassNotFoundException vs NoClassDefFoundError

Both ClassNotFoundException and NoClassDefFoundError errors will be occurred  when the required Class not present in the Class path.
   
ClassNotFoundException
   
ClassNotFoundException is an Exception got arized when trying load class using
forName() method in class Class.
findSystemClass method in class ClassLoader .
loadClass method in class ClassLoader.

e.g. Class.forName("oracle.jdbc.driver.OracleDriver")
if the OracleDriver class file is not present in the Class path, then we will end up with this ClassNotFounException

NoClassDefFoundError

NoClassDefFoundError is an Error thrown at Runtime, when the Class is present during the compile time and not present during Runtime of the Code, then we will end up with this NoClassDefFoundError.

1. When required Class is not available at Runtime available only in compile time.
2.Since NoClassDefFoundError error is an Subclass of LinkageError, it will occur when one of the     Dependent of the jar not present in class path.
3. When the Static initialization of Class failed then it triggers java.lang.ExceptionInInitializerError
 which also leads to this Error.

e.g.  Exception in thread "main" java.lang.NoClassDefFoundError

Comments

Popular posts from this blog

Spring Reactive Stack

Basic Sortings (Bubble, Selection and Insertion Sorts)

public class BasicSortings { public static void main(String[] args) { int temp; int iterationCount = 0; int array[] = { 2, 33, 29, 30, 21, 98}; //Bubble sort or Simple sort for (int i = 0; i < array.length - 1; i++) { for (int j = i + 1; j < array.length; j++) { if (array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } iterationCount++; } } System.out.println("Bubble Sort Big 0(n) --> " + iterationCount); for (int s = 0; s < array.length; s++) { System.out.print(array[s] + "\t"); } System.out.println("\n"); System.out.println("\n"); //Selection Sort iterationCount =0; int sortPointer=0; for (int i = sortPointer; i < array.length; i++) { for(int j=i+1;j< array.length;j++) { if(array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp;

Compress a String

package com.sbs.java8.praticse; public class StringCompression { public StringCompression() { // TODO Auto-generated constructor stub } public static void main(String[] args) { System.out.println(compressString("aaaabbbbbccccAAAAccccccccdefg")); } public static String compressString(String str) { //String str = "aaaabbbbbcccc"; char[] charArray = str.toCharArray(); String compressedString = ""; int i = 0; while (i < charArray.length) { int counter = 1; int j = i + 1; while (j < charArray.length && charArray[i] == charArray[j]) { counter++; j++; i++; } compressedString = compressedString + charArray[i] + counter; i++; } //System.out.println(compressedString); String output =(compressedString.length() > charArray.length)? str: compressedString; return output; } }