| Author |
boolean check when using Class.fromName
|
Meir Yan
Ranch Hand
Joined: Apr 27, 2006
Posts: 597
|
|
Hello all i wander if there is away to check if class exists with but without throwing exception if class not found but boolean value thanks
|
 |
Ajay Singh
Ranch Hand
Joined: Dec 13, 2006
Posts: 182
|
|
You can use the following
|
 |
Meir Yan
Ranch Hand
Joined: Apr 27, 2006
Posts: 597
|
|
hi i tried to avoid the try/catch im doing something similar : i was wandering if there is some build in method that returns boolean value so i could do :
|
 |
Ajay Singh
Ranch Hand
Joined: Dec 13, 2006
Posts: 182
|
|
i tried to avoid the try/catch
Why? Do *not* remove it. It will not figure out whether class is there.
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
|
Yes, it would be nice if you could check for the existence of a class without having to relegate to exception handling. Unfortunately, there isn't.
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
If all the classes you're interested in come from class files (or jar files containing class files) located on the class path, then you might try something like this: Beware that it's not guaranteed to work for all class loaders. Also it doesn't handle nested classes well (can't find java.util.Map.Entry for example). One could fix this making assumptions based on capitalization, but then there's still a problem if you deal with classes that don't follow standard naming conventions. Ultimately, you're probably better off just catching the exception as originally suggested - at least if you want a definite answer.
|
"I'm not back." - Bill Harding, Twister
|
 |
 |
|
|
subject: boolean check when using Class.fromName
|
|
|