I had recently reinstalled my system and was trying to run a simple class that consisted
of a “Hello World” program in Java. I received the following:

Compile:

$> javac LottoMain.java

Run:

$> java LottoMain

Exception in thread “main” java.lang.NoClassDefFoundError: LottoMain
Caused by: java.lang.ClassNotFoundException: LottoMain
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
Could not find the main class: LottoMain. Program will exit.

The compile line completed without errrors and the LottoMain.class file was there, but
what I didn’t know was that I no longer had the current directory “.” in my CLASSPATH variable.

After adding “.” to my CLASSPATH variable the run command gave me what I was expecting.

Run:

$> java LottoMain

Hello Eric!

Updating the CLASSPATH variable in Bash:
in my home directory/.bashrc file

export CLASSPATH=$CLASSPATH:.

Tags: ,

2 comments to “Java Exception java.lang.NoClassDefFoundError and how to resolve it”

  1. Comment by Ahmad:

    I’m trying to run swt-java pcjreot from command prompt (without eclipse) & getting the following exception :Exception in thread “main” java.lang.NoClassDefFoundError: com/beans/MyBean at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) at java.lang.ClassLoader.defineClass(ClassLoader.java:615) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) at java.net.URLClassLoader.access$000(URLClassLoader.java:58) at java.net.URLClassLoader$1.run(URLClassLoader.java:197) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:169) at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:100) at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:70) at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:276) at org.hibernate.cfg.HbmBinder.bindSimpleId(HbmBinder.java:401) at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:334) at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:273) at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:144) at org.hibernate.cfg.Configuration.add(Configuration.java:669) at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:504) at org.hibernate.cfg.Configuration.addResource(Configuration.java:566) at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587) at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508) at org.hibernate.cfg.Configuration.configure(Configuration.java:1428) at com.facadeimplementation.hibernate.util.HibernateFactory.configureSessionFactory(HibernateFactory.java:83) at com.facadeimplementation.model.dao.MyDao.(MyDao.java:23) at com.facadeimplementation.model.dao.MappingDao.(MappingDao.java:23) at com.ui.MainForm.(MainForm.java:45) at com.ui.MainForm$1.run(MainForm.java:77) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) at com.ui.MainForm.main(MainForm.java:74)Caused by: java.lang.ClassNotFoundException: com.beans.MyBean at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) … 37 moreI’m able to run this pcjreot successfully in Eclipse.MyBean.java is contained in a Jar. I have so many POJOs used in the pcjreot so it’s not possible to use them directly instead of using them from within a jar.

  2. Comment by ecdown:

    What is the command line you are attempting to use that gives you this error?

Leave a Reply