private static List < Class < ? > > findClasses ( File dir , String pkgname , boolean flat ) throws ClassNotFoundException { List < Class < ? > > classes = new ArrayList < Class < ? > > ( ) ; if ( ! dir . exists ( ) ) { return classes ; } File [ ] files = dir . listFiles ( ) ; for ( File file : files ) { if ( file . isDirectory ( ) && ! flat ) { classes . addAll ( findClasses ( file , pkgname + "." + file . getName ( ) , flat ) ) ; } else if ( file . getName ( ) . endsWith ( ".class" ) ) { classes . add ( Class . forName ( pkgname + ' ' + file . getName ( ) . substring ( 0 , file . getName ( ) . length ( ) - 6 ) ) ) ; } } return classes ; }
Recursive method used to find all classes in a given directory and subdirectories .