Exception in thread main java.lang.NoClassDefFoundError

Writing by on Monday, 12 of October , 2009 at 9:18 pm

After getting used to the spoon-feeding IDE’s, writing some one-off classes might lead to this Exception.

Exception in thread “main” java.lang.NoClassDefFoundError: HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: HelloWorld. Program will exit.

Very common problem. Your classpath needs a .: followed by the classpath, to include the class(es) in your current directory.

java -cp .:./lib/mysql.jar HelloWorld

Leave a comment

Category: Java

Iterate over Static Fields and Values from a Java Class

Writing by on Monday, 5 of October , 2009 at 4:14 pm

Here’s a method that shows how to get all the static fields in a Java class and return them.

	public static List printFields(Class c) throws ClassNotFoundException, IllegalAccessException {
    		List fields = new ArrayList();
        java.lang.reflect.Field[] f = c.getDeclaredFields();
        for (java.lang.reflect.Field field : f) {
            int modifier = field.getModifiers();
            if (java.lang.reflect.Modifier.isStatic(modifier)){
          		String val = (String) field.get( null );
          		System.out.print(field.getName() +  ": " + val.toString() + ", ");
          		fields.add(val);
            }
        }
        return fields;
    }

Leave a comment

Category: Java

Find out what version a Java Class is compiled with

Writing by on Tuesday, 17 of February , 2009 at 6:12 pm

I got an UnsatisfiedLinkError and wanted to find out whether our JDK upgrade had caused it and did some searching and found that javap -verbose lets you find out the version of a compiled class. So get the class (could be in a JAR file) and then follow this example where I find the version of MyCallback.class

C:>c:\jdk1.6.0_07\bin\javap -verbose MyCallback
Compiled from “MyCallback.java”
public class com.shivdev.MyCallback extends java.lang.Object implements com.shivdev.IFilter
SourceFile: “MyCallback.java”
minor version: 0
major version: 49

Then compare with the following:

major  minor  JDK
45       3    1.0
45       3    1.1
46       0    1.2
47       0    1.3
48       0    1.4
49       0    1.5
50       0    1.6

Alternatively, you can use this java class to find out the version:

public class FindVersion {

    private static int convert(byte upper, byte lower) {
        int ret =  upper << 8;
        ret |= lower;
        return ret;
    }

    public static void main(String[] args) throws Exception {
    		final String CLASS = "MyCallback.class";
        ClassLoader loader = FindVersion.class.getClassLoader();
        java.io.InputStream in = loader.getResourceAsStream(CLASS);
        try {
            byte[] buffer = new byte[8];
            in.read(buffer);
            int minor = convert(buffer[4], buffer[5]);
            int major = convert(buffer[6], buffer[7]);
            System.out.println(major + "." + minor);
        } finally {
            in.close();
        }
    }
}

Leave a comment

Category: Java

Launching Eclipse with More Memory (Increasing Heap Size)

Writing by on Wednesday, 15 of October , 2008 at 11:36 am

I was trying to increase eclipse heap size and setting it to 1024MB and that wouldn’t work. Reducing it to 768MB did the trick for me.

C:\eclipse\eclipse.exe -vmargs -Xmx768M

Later, after installing Your Kit Profiler and trying to add a plug in for eclipse, I started seeing the same error. So I opened up the eclipse.ini and removed the –launcher.XXMaxPermSize 256m option.

Comments (1)

Category: Java

JAR Search Engine – findJAR.com

Writing by on Monday, 8 of September , 2008 at 3:52 pm

findJAR.com is an excellent JAR search engine that helps to find JAR files containing required Java classes. Just enter the name of a class or the JAR file you’re looking and Search. findJAR.com can be used to easily resolve NoClassDefFoundError and ClassNotFoundException problems. Just awesome.

Comments (2)

Category: Java,Rants/Raves

Excellent ANT Tutorial

Writing by on Friday, 11 of July , 2008 at 11:33 am

Here’s and excellent Hello World ANT Tutorial with which you could get started with a decent looking build.xml

Leave a comment

Category: Java

Shivdev Kalambi's Blog

Shivdev Kalambi is a Software Development Manager, previously a Principal Software Engineer at ArcSight/HP. With over 16 years' experience in software development, he's worked on several technologies and played different roles and contributed to all phases of projects. Non-tech activies include Ping-pong, Rock Climbing and Yoga at PG, Golf, Skiing, Swimming & a beer enthusiast.