Reputation: 1254
I have a class file named User.class in D:/classes/directory
and i used
Class clz=Class.forName("D:/classes/User")
But it throws java.lang.ClassNotFoundException
can anybody tell me how can i resolve this problem.
Upvotes: 2
Views: 192
Reputation: 632
try Class.forName("User") but note that you should call it from same directory OR add to classpath your User.class also check http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html#forName(java.lang.String) if you using packages then try to add Class.forName("com.example.User")
Upvotes: 0
Reputation: 346260
You need to use the fully qualified name of the class, which includes its package. This is then typically used to look in the classpath, with directories corresponding to package parts. In your case, if the classpath includes D:\classes
and the class User
is in the package directory
, this would work: Class.forName("directory.User");
Upvotes: 1
Reputation: 106
forName requires the package path. What package do you want User to belong to? Here is an excellent example I found after a little bit of googling: http://www.xyzws.com/Javafaq/what-does-classforname-method-do/17
Upvotes: 0
Reputation: 240860
Class.forName(className)
expects class to be in classpath and you can give fully qualified class name there
Note : className
Upvotes: 2