views:

73

answers:

2

In C# you can easily read all classes from a given assembly.

I'm looking for equivalent feature in Java. I need this to automatically bind EJB beans to my Guice Module.

+2  A: 

The Java equivalent of an assembly is a JAR file; the contents of a JAR file can be listed by using the classes in java.util.jar.

There is no more general mechnism (like getting all classes in a package) because the Java class loading mechnism is very flexible and allows for things like classes loaded via HTTP or generated on the fly, so it's impossible to know all classes that are available.

Michael Borgwardt
+3  A: 

Unfortunately there seems to be no ready-to-use solution in Java.

In our project we used a slightly modified approach suggested by some blog. The solution described there is to scan file system and JARs for classes.

If you want to pick only classes implementing some interface, you can do some additional check like clazz.isAssignableFrom(MyInterface.class).

Grzegorz Oledzki
It helped my a lot. Thanks!
mgamer