0

I am trying to get a class and call a method of that class. I can get the resource, from that i have a File and from that an absolute path to my class, but i can not get the actual class out of it so that i can call the method. How could i get the class from the resource/file/path? Class.forName gives---> "java.lang.ClassNotFoundException"

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("classpath*:com/company/worker/module/*.class");
log.info("found RES::  " + Arrays.toString(resources));
Arrays.asList(resources).forEach(r -> {
    try
    {//////////////////////////Class.forName gives---> "java.lang.ClassNotFoundException"///////
        Method handleRequest = Class.forName(r.getFile().getAbsolutePath()).getDeclaredMethod("handleRequest",
            Parameter.class, java.lang.String.class);
        handleRequest.invoke(searchParameter, myType);
    }
    catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException | NoSuchMethodException
        | ClassNotFoundException | IOException e)
    {
        e.printStackTrace();
    }
});
ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
Daniel Jeney
  • 486
  • 1
  • 5
  • 19
  • 2
    `Class.forName` expects the fully qualified name of the class, not the file path. So you should be converting the resource name to `"com.company.worker.module."` somehow. – Slaw Jun 16 '20 at 06:43
  • @Slaw that does work, thank you!(answer so i can accept) – Daniel Jeney Jun 16 '20 at 06:49
  • 1
    I only provided a hint. Since you figured out the full solution it would probably be best for you to answer your own question so this Q&A can potentially help someone else in the future. – Slaw Jun 16 '20 at 06:51

0 Answers0