jeudi 18 juillet 2019

Load external classes

I am trying to load external classes in my project. My idea is to load a class and then use reflection to get its properties and methods.

I am trying to do this, but it doesnt work

        Path destination = Paths.get("C:\\Project\\src\\com\\test\\TestLauncher.java");

        URL[] classUrls = new URL[]{ new File(destination.subpath(0, destination.getNameCount()).toString()).toURL() };

        URLClassLoader ucl = new URLClassLoader(classUrls);

        Class<?> beanClass = ucl.loadClass("com.test.FileToLoad");

This code works as a charm when you try to use it in files in the same project but it doesn't work when the class is not in the same project, in this case I get the following error:

java.lang.ClassNotFoundException: com.test.FileToLoad at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at urlLoader.Play.main(Play.java:26)





Aucun commentaire:

Enregistrer un commentaire