Home Navigation

Saturday 12 September 2020

ClassNotFoundException vs NoClassDefFoundError

ClassNotFoundException and NoClassDefFoundError both are runtime exceptions. They occur when a class not found in the classpath. 

ClassNotFoundException:

It is a checked exception. It happens when a program tries to load a class using the Class.forName() or loadClass() or findSystemClass() method. For example class.forName("oracle.jdbc.driver.OracleDriver") and oracle jdbc driver is not present in the classpath the program will try to load the class and throw the classNotFoundException.

Resolution: Make sure you add the related dependencies in the classpath.

NoClassDefFoundError:

It is a fatal error and happens when jvm can not find the definition of the class by instantiating (new keyword) and load a class with method call. The definition is present at the compile time but missing at runtime.

It usually happens when there is an exception while executing a static block or initializing static fields of the class, so class initialization fails.

Resolution: Sometimes, it can be quite time-consuming to diagnose and fix these two problems. 

  • Make sure whether class or jar containing that class is available in the classpath.
  • If it's available on application's classpath then most probably classpath is getting overridden. To fix that we need to find the exact classpath used by our application
  •  Also, if an application is using multiple class loaders, classes loaded by one classloader may not be available by other class loaders.

Ref: https://www.baeldung.com/java-classnotfoundexception-and-noclassdeffounderror

Tuesday 1 September 2020

Git stash commands

#git stash save “Your stash message” //Git stash with message

Stashing untracked files
#git stash save -u
or
#git stash save --include-untracked

view the list of stashes you made at any time.
#git stash list

#git stash apply // applies the latest stash stash@{0}

if you want some other stash to apply
#git stash apply stash@{2} // third one

#git stash pop   // applies the latest stash stash@{0} and removes it
#git stash pop stash@{1} // applies the second one and removes it


#git stash show // summary of stash diff of the stash content
#git stash show -p // shows full diff of the stash content
#git stash show stash@{1} // specific stash diff and contents

#git stash branch <name> // creates a new branch with latest stash and removes it
#git stash branch <name> stash@{1} // if you want to specify a stash id

#git stash clear // deletes all the stashes made in the repo
#git stash drop stash@{2} // specify id to delete the stash