tags:

views:

66

answers:

1

I have a project created in Eclipse, and I defined an interface and a class for dynamic class loading, the class is in the project directory, so I have this code in my project:

if (handlerClassName != null) 
    {
         TypeHandler typeHandler = null;
         try {
             typeHandler = (TypeHandler) 
            (Class.forName(handlerClassName).newInstance());

but I get this exception: java.lang.ClassNotFoundException: "handlerClassName"

what should I do to make the JVM recognize the class "handlerClassName" in my project?

thanks

A: 

Use the full qualified classname including the entire package, e.g. java.lang.String instead of the short name like string. If it is actually not in a package, then fix it so that it's in a package. Packageless classes are namely invisible for classes in a package.

BalusC
Thanks Balusc, you're right, I had to change it to (Class.forName("homework4." + handlerClassName).newInstance());and it's working now.
Noona
You're welcome. According to the [Sun Java Naming Conventions](http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html), however, classnames ought to start with UpperCase.
BalusC
I have it capitalised in the project, handlerClassName was just an alias.
Noona
Fine. In the future try to avoid this kind of red herrings :)
BalusC
herrings? :), okay, noted.
Noona
I had another question but i know the answer now.
Noona