Exception in thread main java.lang.NoClassDefFoundError

Writing by shivdev 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 shivdev 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

Generate Timestamps in MySQL

Writing by shivdev on Thursday, 1 of October , 2009 at 5:20 pm

So you want to populate a new column in MySQL with fake dates?

Here’s how to add a new datetime column in SQL

alter table city_new add column mytime datetime;

Here’s how to populate it with fake timestamps data

update city_new set mytime = select from_unixtime(
unix_timestamp(’2009-01-01 01:00:00′)+floor(rand()*31536000)
);

Here’s a more generic formula

select from_unixtime(
unix_timestamp( ‘start timestamp’)
+floor(rand()* (max interval in seconds) )
);

Leave a comment

Category: Tips and Tricks

Shivdev Kalambi's Blog

Shivdev Kalambi is a Principal Software Engineer working at ArcSight. He has over 11 years' experience in software development and has worked with several technologies and played different roles and contributed to all phases of projects. Other non-tech activies include Ping-pong, Racquetball, Rock Climbing at PG and Swimming.