-
Class.forName() -- class not found, why?
Hi,
I'm actually trying to see how I could invoke instances of classes for which
I known the name only at runtime. So I thought to use the "forName()" method
of the "Class" class. Unfortunately, when running my test code, I always
get an exception saying that the class could not be found. Anyone could give
me a tip how to use that "forName" method.
Here's my test code (2 classes):
class RunMe {
public static void main(String args[]) {
try {
System.out.println("Loading the class");
Class oClass = Class.forName("LoadedClass.class");
}
catch (Exception e) {
System.out.println(e);
}
}
}
class LoadedClass {
public static void main(String args[]) {
System.out.println("i'm the loaded class");
}
}
I also tried to package the "LoadedClass.class" in a jar file, add it to
my classpath but I got the same result.
PS: Sorry if this is a stupid question, I'm starting to really look how to
program in Java.
Regards,
-Olivier.
-
Re: Class.forName() -- class not found, why?
the problem is that you give the ".class" extension also. try without giving
the extension it will work fine.
the line should be
Class oClass = Class.forName("LoadedClass");
give the name of the class only 
Regards
Karthikeyan.R
"Olivier Béghain" <olivier.beghain@be.ibm.com> wrote in message
news:3f1da343$1@tnews.web.devx.com...
>
> Hi,
>
> I'm actually trying to see how I could invoke instances of classes for
which
> I known the name only at runtime. So I thought to use the "forName()"
method
> of the "Class" class. Unfortunately, when running my test code, I always
> get an exception saying that the class could not be found. Anyone could
give
> me a tip how to use that "forName" method.
>
> Here's my test code (2 classes):
>
> class RunMe {
> public static void main(String args[]) {
> try {
> System.out.println("Loading the class");
> Class oClass = Class.forName("LoadedClass.class");
> }
> catch (Exception e) {
> System.out.println(e);
> }
> }
> }
>
>
> class LoadedClass {
> public static void main(String args[]) {
> System.out.println("i'm the loaded class");
> }
> }
>
> I also tried to package the "LoadedClass.class" in a jar file, add it to
> my classpath but I got the same result.
>
> PS: Sorry if this is a stupid question, I'm starting to really look how to
> program in Java.
>
> Regards,
> -Olivier.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|