2013-03-04 17 views

cevap

11
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); 

Kaynak: tam olarak JARs bulunduğu https://stackoverflow.com/a/60775/1360074

+0

Gerçekten kullanmamalısınız 'Sınıf # newInstance()' daha iyi olur Class # getConstructor() # newInstance() 'yi kullanmak için –

+0

Teşekkür ederim NiKolay Kusznetov, Cevap int Kaynak tartışmasını buldum :) –

2

Böyle bir şey deneyebilirsiniz, ama bilmeni gerektirir. bir bildirilmemiş kontrol istisna http://docs.oracle.com/javase/tutorial/reflect/member/ctorTrouble.html tanıtmak çünkü

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"); 
İlgili konular