Hi I am new to javamail API. I added mail.jar and activation.jar in my jre/lib/ext.But still when I am trying to run programs I am getting an error of java.lang.NoClassDefFoundError in main .. can anyone help !! i really need to begin up coding as the deadline's approaching

Dani AI

Generated

Context: sees a runtime java.lang.NoClassDefFoundError when trying to use JavaMail; correctly suggested adding libraries through the IDE rather than fiddling with the JRE install. Below is a compact, practical checklist to find exactly why the JVM cannot load the class and how to fix it reliably.

First, identify the missing class from the stacktrace (the error line usually shows a fully qualified name such as javax/mail/SomeClass). Use that name to verify which JAR should contain it and whether that JAR is intact. A quick way to inspect a JAR from a shell is:

jar -tf mail.jar

If the reported class is not listed, the wrong JAR version or a corrupted JAR is the likely cause.

Next, confirm the runtime classpath actually contains the JARs used at compile time. Running from the command line forces clarity:

# Unix/mac
java -cp mail.jar:activation.jar:. com.example.Main

# Windows
java -cp mail.jar;activation.jar;. com.example.Main

If running from an IDE, add the JARs to the project-level libraries (not only to the JRE install). In NetBeans use Project -> Properties -> Libraries -> Add JAR/Folder. In Eclipse use Project -> Properties -> Java Build Path -> Libraries -> Add JARs / Add External JARs. For web apps, place dependencies in WEB-INF/lib or in the server-level lib when appropriate.

Additional causes to check: the app may be using a different JRE/JDK than the one modified (verify the runtime JRE configured in the IDE), a dependent class inside another JAR is missing, or a static initializer failed earlier (look for ExceptionInInitializerError). Avoid relying on the JRE ext folder—it hides classpath issues and reduces portability. Diagnose with the exact missing-class name, confirm the JAR contents, and then place the needed JARs on the runtime classpath at the project/server level.

Recommended Answers

All 3 Replies

You should added the library through your IDE not to mess with Java installation. For example in Netbeans from main menu choose Tools >> Java Platforms . Select a platform you want to add resources for example JKD1.6, select Sources tab and click on Add JAR/Folder. Through file chooser you will navigate to the location of the JAR file and select it.

Similar process is available on every IDE that I used so far

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.