0

Given a package, is it possible to find all the classes in it?

For instance, how do you find out what classes are contained within java.lang?

Thanks

1 Answers1

0

This should get you started (look at the console output, it will make sense):

import com.google.common.collect.Multimap;
import org.reflections.Reflections;

import java.util.Map;

public class PackageWalker {
    public PackageWalker() {}

    public void walk() {
        Reflections reflections = new Reflections("org.reflections");
        for (String mmapName : reflections.getStore().getStoreMap().keySet()) {
            System.out.println("KEY["+mmapName+"]");
            Multimap<String,String> mmap = reflections.getStore().getStoreMap().get(mmapName);
            for (Map.Entry<String,String> entry: mmap.entries()) {
                System.out.println("  PAIR[ "+entry.getKey()+"="+entry.getValue()+" ]");
            }
        }
    }

    public static void main(String[] args) {
        new PackageWalker().walk();
    }
}

Following jars and their dependencies are required (using Ivy format):

    <dependency org="org.reflections" name="reflections" rev="0.9.8"/>

Here is my project folder: http://www.filedropper.com/laboratorytar

You just need to make sure Ivy is installed with ant (essentially put ivy.jar into your ANT_HOME/lib (or ~/.ant/lib/ on *nix) and it will just work).

Alex Chacha
  • 423
  • 2
  • 7
  • BUt my project isn't a maven project. I've downloaded the jar file from the google page, but couldn't figure out how to put it in my ant project – user1801813 Feb 13 '13 at 02:55
  • I just included that as a reference for what you need to make it work. You should look into Ivy and ant, very easy to set up and it will do dependencies for you so you don't have to manage jars (it took me maybe 30 minutes to get it working: http://ant.apache.org/ivy/ Ivy is so much easier than Maven). Take all the jars, put them into some lib folder and when you run it from ant, add those jars to the classpath (the usual way you would do external jars). – Alex Chacha Feb 13 '13 at 16:33