I need load a jar file in runtime in Java and I have this code but it not load any jar and I don't know how, somebody can tell me why? I have JVM 8 and NetBeans 8, the purpose is create a program that can load jar files as a plugins y for Windows.
package prueba.de.classpath;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
public class PruebaDeClasspath {
public static void main(String[] args) {
try {
Class.forName("PluginNumeroUno");
} catch (ClassNotFoundException e) {
System.out.println("Not Found");
}
try {
URLClassLoader classLoader = ((URLClassLoader) ClassLoader
.getSystemClassLoader());
Method metodoAdd = URLClassLoader.class.getDeclaredMethod("addURL",
new Class[]{URL.class});
metodoAdd.setAccessible(true);
File file = new File("plugins/PrimerPlugins.jar");
URL url = file.toURI().toURL();
System.out.println(url.toURI().toURL());
metodoAdd.invoke(classLoader, new Object[]{url});
} catch (Exception e) {
e.printStackTrace();
}
try {
Class.forName("PluginNumeroUno");
System.out.println("ok");
} catch (ClassNotFoundException e) {
System.out.println("Not Found");
}
}
}
java --classpathnot doing the trick?