0

Hello stackoverflow'ers! I am trying to list all classes from an interface in a specific package. I came across multiple solutions and tried the following:

Using Reflections:

AllCommands = new ArrayList<ICommand>();
Reflections reflections = new Reflections(this.getClass().getPackage().getName());
commandClasses = reflections.getSubTypesOf(ICommand.class);
for (Class<? extends ICommand> c :commandClasses){
    AllCommands.add((ICommand)c.newInstance());
}

Using extcos:

AllCommands = new ArrayList<ICommand>();
ComponentScanner scanner = new ComponentScanner();
scanner.getClasses(new ComponentQuery() {
    @Override
    protected void query() {
        select().
        from(this.getClass().getPackage().getName()).
        andStore(thoseImplementing(ICommand.class).into(commandClasses)).
        returning(none());
    }
});
for (Class<? extends ICommand> c :commandClasses){
    AllCommands.add((ICommand)c.newInstance());
}

Both solutions are successfully listing all my classes when debugging in NetBeans (7.3), but when i compile the jar and execute it, the commandClasses collections seems to stay empty. I am not used to java but i guess it has to do something with the classpath, and the debugging-time taking the classes from the \target\classes folder and not from the jar.

Can someone help me out with a solution that would allow me to list classes in the .jar? Is it possible that this.getClass().getPackage().getName() just has to change to something that points into the jar?

Sven Mawby
  • 645
  • 6
  • 16

1 Answers1

0

I figured it out myself.. Using this.getClass().getPackage().getName() as Packagename does only point at the files in the classes folder, to reference classes in the jar i needed to replace the '.' to '/'. Thank you anyway.

Sven Mawby
  • 645
  • 6
  • 16