This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes boolean check when using Class.fromName Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "boolean check when using Class.fromName " Watch "boolean check when using Class.fromName " New topic
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: boolean check when using Class.fromName
 
Similar Threads
scjp question
boolean comparision
formatting
How to interpret := syntax in UML
Stopping a Thread