JAVA-通过接口读取本地jar包,并调用其中实现了接口的类中的方法



URLClassLoader classLoader = new URLClassLoader(new URL[]{new URL("jar 本地路径,例如:file:D:/project/inputsplit-1.0-SNAPSHOT.jar")}, Thread.currentThread().getContextClassLoader()); Reflections reflections = new Reflections("your interface package", classLoader); Set<Class<? extends "your interface">> subTypesOf = reflections.getSubTypesOf("your interface"); //从继承中获取任意实现 subTypesOf.stream().findAny().ifPresent(clazz -> { try { "your interface" interface = clazz.newInstance(); } catch (Exception e) { throw new RuntimeException(); } });

  

猜你喜欢

转载自www.cnblogs.com/a77355699/p/10234939.html