Using the system ClassLoader with Class.forName()

L

learningjava

Hi,

Part of my looks like this:

Class c = null;
c = Class.forName("Car");
//class Car is defined in this file itself
Object object = c.newInstance();
ArrayList arg = new ArrayList();
ArrayList types = new ArrayList();
arg.add (new String("description"));
types.add(String.class);
Object[] argValues=arg.toArray();
Class[] argtypes=(Class[])types.toArray(new Class[argValues.length]);
Method method;
method=c.getMethod("description", argtypes);

The whole code compiles fine.
But I get a runtime error which says there was a ClassNotFoundException.
I have changed the CLASSPATH to include the directory in which these
programs sit, but am still getting the errors.

Help, please! Why doesn't it find my class?
 
A

Anton Spaans

It looks like the 'Car' class is not fully qualified.....
So, add the package name to the "Car" string. Say this "file" is a piece of
code in package "org.foo".
Then do:

c = Class.forName("org.foo.Car");

Even better would be just this, if Car is in this file anyway:

c = Car.class;

-- Anton.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top