• Post Reply Bookmark Topic Watch Topic
  • New Topic

boolean check when using Class.fromName  RSS feed

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the following


 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!