0

My code is giving me a java.lang.ClassNotFoundException: a.

How can I generate that class?

Dre
  • 4,298
  • 30
  • 39
user622222
  • 131
  • 1
  • 1
  • 8
  • what's in the `line` variable exactly? – Bozho Feb 21 '11 at 12:11
  • @user622222 ... now - _what is the value of this string_ – Bozho Feb 21 '11 at 12:15
  • [ClassNotFoundException](http://download.oracle.com/javase/1.4.2/docs/api/java/lang/ClassNotFoundException.html) thrown by [Class#forName(String)](http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html#forName%28java.lang.String%29)means that this class is not in your classpath. In some rare cases, it just means that your IDE (if you're using one) has stuck and needs a restart or a "clean/rebuild" of your project. Additionally, when posting a question involving some exception, it's good to report your stack trace to facilitate troublishooting. – Pantelis Sopasakis Feb 21 '11 at 12:18
  • I strongly recommend to let those classes implement a known interface so that you don't need to fiddle with ugly reflection hacks. See also http://stackoverflow.com/questions/2946338/how-do-i-instantiate-class-dynamically-in-java/2946402#2946402 – BalusC Feb 21 '11 at 12:29
  • It is unclear whether you are unable to find an existing class which is what your code is attempting to do, or you want to be able to generate the class dynamically which is what your question implies "How can i generate that class?" – Peter Lawrey Feb 21 '11 at 12:36
  • **A general tip:** when you get some error, you should post the complete error message here, not only the part you think is important. – Paŭlo Ebermann Feb 21 '11 at 12:54

2 Answers2

1

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.

jmj
  • 237,923
  • 42
  • 401
  • 438
0

Do you really want to create a new class at runtime? If so, then

  1. you'll need the bytecode of the class and
  2. create a new ClassLoader which can use this byte code for creating your class.

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.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210