public static List < Class < ? > > getComponentClasses ( URL jar ) throws IOException { JarInputStream jarFile = new JarInputStream ( jar . openStream ( ) ) ; URLClassLoader cl = new URLClassLoader ( new URL [ ] { jar } , Thread . currentThread ( ) . getContextClassLoader ( ) ) ; List < Class < ? > > idx = new ArrayList < Class < ? > > ( ) ; JarEntry jarEntry = jarFile . getNextJarEntry ( ) ; while ( jarEntry != null ) { String classname = jarEntry . getName ( ) ; // System.out.println(classname); if ( classname . endsWith ( ".class" ) ) { classname = classname . substring ( 0 , classname . indexOf ( ".class" ) ) . replace ( ' ' , ' ' ) ; try { Class < ? > c = Class . forName ( classname , false , cl ) ; for ( Method method : c . getMethods ( ) ) { if ( ( method . getAnnotation ( Execute . class ) ) != null ) { idx . add ( c ) ; break ; } } } catch ( ClassNotFoundException ex ) { ex . printStackTrace ( ) ; } } jarEntry = jarFile . getNextJarEntry ( ) ; } jarFile . close ( ) ; return idx ; }
Get all components from a jar file