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?

2
  • ? how should i do that
    – Sven Mawby
    Jun 9, 2013 at 16:37
  • "how should i do that" Do what? You'll have to be more specific as I don't intend writing a tutorial. Jun 9, 2013 at 16:59

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.