如何在运行时动态地将外部jar文件添加到ClassPath?
我想使用java代码动态地将jar文件添加到我的项目的类路径中,如果可能的话,我想使用外部jar文件并加载它们的类,然后将它们作为Beans执行(Spring框架)。
谢谢 :)
URLClassLoader child = new URLClassLoader (myJar.toURL(), this.getClass().getClassLoader()); Class classToLoad = Class.forName ("com.MyClass", true, child); Method method = classToLoad.getDeclaredMethod ("myMethod"); Object instance = classToLoad.newInstance (); Object result = method.invoke (instance);
资料来源: https : //stackoverflow.com/a/60775/1360074
你可以试试这样的东西,但它要求你知道你的JARs
里。
URLClassLoader cl = URLClassLoader.newInstance(new URL[] {myJarFiles}); Class myClass = cl.loadClass("com.mycomp.proj.myclass"); Method printMeMethod = myClass.getMethod("printMe", new Class[] {String.class, String.class}); Object myClassObj = myClass.newInstance(); Object response = printMeMethod.invoke(myClassObj, "String1", "String2");
- 如何在Java中配置.dll文件?
- 系统类加载器是否加载了类路径中的所有类,即使它们实际上没有被使用?
- Weblogic 10.3.1.0正在使用com.bea.core.apache.commons.net_1.0.0.0_1-4-1.jar …我想从我的代码中使用commons-net-2.0.jar
- 是否可以在javac(JDK 9)中混合使用–class-path和–module-path?
- 如何向classpath添加资源
- 如何配置Spring bean容器来加载Java属性文件?
- 执行JAR时java.lang.ClassNotFoundException:com.mysql.jdbc.Driver
- 在eclipse luna中使用JGit时获取NoClassDefFoundError
- 是否可以动态加载包中包含的所有属性文件? 即MyClass.class.getResource(’*。properties’);