My code is giving me a java.lang.ClassNotFoundException: a.
How can I generate that class?
My code is giving me a java.lang.ClassNotFoundException: a.
How can I generate that class?
I didn't do that. It's giving error like that; java.lang.ClassNotFoundException: a How can i generate that class?
String line = reader.readLine();
Class<?> writeoutClass = Class.forName(line);
The class you are entering here , isn't available in classpath.
if you enter as String:helloasdjfhajsdklfhjh there should be class in the class path with same qualified name.
Do you really want to create a new class at runtime? If so, then
For 1: You could, for example, take the bytecode from some database or something, if it is already existent. Otherwise, you'll need to create it - either by generating java source code and compiling it (which means a Java compiler needs to be available), or by using a bytecode engineering library like ASM. (Of course, you could also do this by hand, but I would not recommend it.)
This all depends on what you actually want to do.
For 2: Create a subclass of ClassLoader and implement the findClass method to get the bytecode and invoke defineClass for you. Create an object of this class, and pass it as an argument to the Class.forName() method, or simply invoke loader.loadClass(name).
If your new class uses any other classes, they should be loaded either by the same classloader, or any ancestor classloader, otherwise you'll get runtime errors.