I want to initialize a class that is not known during compilation (yet implementing a known interface).
So I tried something like this :
Class<?> cls = class.foreName("NotKnown",true,ClassLoader.getSystemClassLoader());
It worked in Eclipse , but as a runnable jar file I found out this won’t work because it won’t load a class which is out of your classPath.
How can I make it work ?
You need to create new classloader that would load classes from the folder where your new class is located:
I.e. if you have a class
foo.bar.Bazand it was compiled to/someFolder/foo/bar/Baz.class, you should usenew File("/someFolder").toURI().toURL()as a folder to load your class from.