Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Getting error Exception in thread "main" java.lang.NoClassDefFoundError

  Asked By: Ayden    Date: May 14    Category: Java    Views: 1507

I have compiled this program. I got following errors:

Exception in thread "main" java.lang.NoClassDefFoundError: Utils
(wrong name: utils/Utils)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:486)
at java.security.SecureClassLoader.defineClass
at java.net.URLClassLoader.defineClass
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:297)
at sun.misc.Launcher$AppClassLoader.loadClass
at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
at java.lang.ClassLoader.loadClassInternal

What is the problem?
package utils;

import java.text.*;
import java.io.*;
import java.util.*;

public class Utils
// Variables

// Main function
public void main()
String pNum = "New York", pDate = "30/02/2002",
pFormat = "DD/MM/YYYY", pEmail = "NY@...";
if (isValidNum(pNum))
{ System.out.println("Valid string : " + pNum); }
else { System.out.println("Invalid string : " + pNum); }

if (isValidDate(pDate, pFormat))
{ System.out.println("Valid date : " + pDate); } else
{ System.out.println("Invalid date : " + pDate); }

if (isValidEmail(pEmail))
{ System.out.println("Valid email : " + pEmail); }
else { System.out.println("Invalid email : " + pEmail); }

// Functions
public boolean isValidNum(String pNum)
try {
return true;
catch(NumberFormatException e){
return false;

public boolean isValidDate(String pDate, String pFormat)
DateFormat dateformatfrm = new SimpleDateFormat
return true;

catch (ParseException e)
return false;

public boolean isValidEmail(String pEmail)
int vIndex1, vIndex2;
vIndex1 = pEmail.indexOf("@");
vIndex2 = pEmail.indexOf(".", vIndex1);
if ( (vIndex1 > 0) && (vIndex2 > vIndex1) )
return true;
return false;



3 Answers Found

Answer #1    Answered By: Burk Martin     Answered On: May 14

Try this:


public static void  main(String[] args){

public static boolean isValidNum(...){

public static boolean isValidDate(...){

public static boolean isValidEmail(...){

And the result is as:

Invalid string  : New York
java.text.ParseException: Unparseable date: "30/02/2002"
Invalid date  : 30/02/2002
Valid email  : NY@y...
Process exited with exit code 0.

My understanding is that the main  method has to be defined as
public static void main(String[] args)

And the methods called in main() are also to be defined as "static".

Answer #2    Answered By: Hubert Taylor     Answered On: May 14

Your understanding about static and not static is right, but the error  has
nothing to do with it. The error you mention can only be debugged if you give us
the method implementation code. But from my experience, without looking at the
code, I assume that problem  is because you set the date  as 30/02/2002, in
American Locale "30" would be treated as month which is illegal!

Got the point, change the date as 02/30/2002 for checking, if it works then
re-write your code.

Answer #3    Answered By: Lurlene Fischer     Answered On: May 14

I do not know what method implementation code means? Please let me
By the way I have compiled  this java  code and just run: "javac Utils"
from command prompt.