aspose file tools*
The moose likes Java in General and the fly likes ClassNotFoundException while using Class.forName to load a class dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ClassNotFoundException while using Class.forName to load a class dynamically" Watch "ClassNotFoundException while using Class.forName to load a class dynamically" New topic
Author

ClassNotFoundException while using Class.forName to load a class dynamically

sreenivas vemula
Ranch Hand

Joined: Jan 21, 2012
Posts: 44
The following code is throwing ClassNotFoundException:

public Object getPropertyObj(String key) throws Exception {
Class cls = Class.forName(key);
return cls.newInstance();
}

Please suggest...

Thanks in advance.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3689
    
  16
The key variable contains the name of a class that is not on your classpath.
The first thing you should do is print out the value of key to make sure it contains what you think it contains.


Joanne
sreenivas vemula
Ranch Hand

Joined: Jan 21, 2012
Posts: 44
could you please correct me if I am adding a class to a classpath in a right way or not?

system --> advanced --> environmental variables --> classpath

under this I am adding the location of a file

my jave file is located under the directory say D:\proj\pck

then what should be the variable value that needs to be added to classpath variable name.


Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

You're much better off not setting the environment classpath at all, and setting the classpath in the java command line using the -cp option.

Is the class in a package? The classpath ought to contain the root of the package structure. So if, for instance, the class file is in D:\proj\pck, but it's in the package pck, then the classpath ought to contain D:\proj
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3689
    
  16
sreenivas vemula wrote:my jave file is located under the directory say D:\proj\pck

It's the location of the class file that is important, not the java file
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassNotFoundException while using Class.forName to load a class dynamically