How to check if class exists?

J

Jacob

In my main window I set up certain things based on the
availability of certain classes in my system.

I use Class.forName() to check if a certain class exists,
basically like this:

public boolean exists (String className)
{
try {
Class.forName (className);
return true;
}
catch (ClassNotFoundException exception) {
return false;
}
}

Apparently the forName() method does a lot of stuff I don't
want it to do at this point, like loading dependent classes
etc. I've also learned from a previous thread that it also
executes static initializers and so on.

Question is: Is there a more "lightweight" way to check if
a class exists or not?

Thanks!
 
A

Andrew Thompson

| In my main window I set up certain things based on the
| availability of certain classes in my system.
|
| I use Class.forName() to check if a certain class exists,
| basically like this:
....
| Apparently the forName() method does a lot of stuff I don't
| want it to do at this point, like loading dependent classes
| etc. I've also learned from a previous thread that it also
| executes static initializers and so on.

What thread, URL?
 
S

Steve Horsley

Jacob said:
In my main window I set up certain things based on the
availability of certain classes in my system.

I use Class.forName() to check if a certain class exists,
basically like this:

public boolean exists (String className)
{
try {
Class.forName (className);
return true;
}
catch (ClassNotFoundException exception) {
return false;
}
}

Apparently the forName() method does a lot of stuff I don't
want it to do at this point, like loading dependent classes
etc. I've also learned from a previous thread that it also
executes static initializers and so on.

Question is: Is there a more "lightweight" way to check if
a class exists or not?

Thanks!

A wild guess: Try tinkering with Classloader.getResource("Thingy.class")
and see if you can open class files as input.

Steve
 
J

Jacob

Andrew said:
| In my main window I set up certain things based on the
| availability of certain classes in my system.
|
| I use Class.forName() to check if a certain class exists,
| basically like this:
...
| Apparently the forName() method does a lot of stuff I don't
| want it to do at this point, like loading dependent classes
| etc. I've also learned from a previous thread that it also
| executes static initializers and so on.

What thread, URL?

http://groups.google.com/[email protected]
 
A

Andrew Thompson

| Andrew Thompson wrote:
| > | > | In my main window I set up certain things based on the
| > | availability of certain classes in my system.
| > |
| > | I use Class.forName() to check if a certain class exists,
| > | basically like this:
| > ...
| > | Apparently the forName() method does a lot of stuff I don't
| > | want it to do at this point, like loading dependent classes
| > | etc. I've also learned from a previous thread that it also
| > | executes static initializers and so on.
| >
| > What thread, URL?
|
|
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=t8skevc634
c8994er55eut76ufi5uupvvc%404ax.com
|

I think you read too much into what was
being written. Lothar only mentioned the
initialiser..

Then I went to the JavaDocs, (very handy
things) and noticed this in the comments..

Invoking this method is equivalent to:
Class.forName(className, true, currentLoader)

So, it would seem that..
Class.forName("findThisClass", false, null);
...is the answer you actually seek?

HTH

--
Andrew Thompson
* http://www.PhySci.org/codes/ Web & IT help
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
 
B

bluke

Snipped
So, it would seem that..
Class.forName("findThisClass", false, null);
..is the answer you actually seek?

HTH

This is not really lightweight because if the class is not found it
throws an exception which is expensive. In addition, you are using an
exception for control flow which is not a good thing. What I would
really like is a method that would allow me to determine if a class is
present or not without throwing an exception.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,282
Latest member
RoseannaBa

Latest Threads

Top