How to download and add .class/.jar file dynamically in java runtime class path Spring Boot 3.x

25 views Asked by At

How to add .class/.jar file dynamically in java class path Spring Boot 3.x version.

I tried like that below, but not working.

File file = Paths.get(getClass().getResource("/Myclass.class").toURI()).toFile();
ClassLoader cl = ClassLoader.getSystemClassLoader();
Class<?> clazz = cl.getClass();

// Get the protected addURL method from the parent URLClassLoader class
//Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] {URL.class});
Class<?>[] paraTypes = new Class[1];
paraTypes[0] = URL.class;
Method method = clazz.getSuperclass().getDeclaredMethod("addURL", new Class[]{URL.class});
method.setAccessible(true);
method.invoke(ClassLoader.getSystemClassLoader(), new Object[ {file.toURI()});
0

There are 0 answers