Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if class exists without running into ClassNotFoundException

Tags:

java

In order to schedule the execution of a job, i get the name of a class as a string-input.
This class may be in one of two packages, but i don't know which one so i have to check this.

By now, i have two try-catch-blocks

Class<Job> clazz;
String className; //the above mentioned string, will be initialized

try {
    clazz = (Class<Job>) Class.forName("package.one." + className);
} catch (ClassNotFoundException ex) {
    try {
        clazz = (Class<Job>) Class.forName("package.two." + className);
    } catch (ClassNotFoundException ex1) {
      //some error handling code here, as the class is
      //in neither of the two packages
    }
}

For more packages this will get uglier and more unreadable. Furthermore, it is - for me - against the concept of exceptions, as exceptions should'nt be expected/used for flow-control!
Is there any way to rewrite this without the utilization of the ClassNotFoundException?

like image 628
KeyNone Avatar asked Nov 06 '13 10:11

KeyNone


People also ask

How do you know if a class is present in classpath?

We can check for the existence of a class using Java Reflection, specifically Class. forName(). The documentation shows that a ClassNotFoundException will be thrown if the class cannot be located.

What is class forName Java?

forName(String name, boolean initialize, ClassLoader loader) method returns the Class object associated with the class or interface with the given string name, using the given class loader. The specified class loader is used to load the class or interface.

What is Java Lang ClassNotFoundException?

ClassNotFoundException is a checked exception and occurs when the Java Virtual Machine (JVM) tries to load a particular class and the specified class cannot be found in the classpath. In older days, there are no editors like Eclipse are available.


1 Answers

I'd stick to the Class.forName method for that.

You can store the class and package names in Collections or Sets and loop through those elements. When you get a ClassNotFoundException, you just continue your search. If you don't get an exception, you exit the loop using break, as you have found the class you were looking for.

The reason I'd go for Class.forName is that it loads the class for you if it had not been already loaded by the VM. This is quite a powerful side effect.

It basically relieves you of the trouble of digging through the whole CLASSPATH and looking for class files to load in the VM and dealing with issues such as whether the class has already been loaded or not by the VM.

EDIT:

Jakob Jenkov has written some really great articles/tutorials on Java. I've found them extremely useful when dealing with reflection, class loaders and concurrency (some of the "hardest" aspects of Java).

Here's a good article on the Java class loader by him, for if you still decide not to use Class.forName.

like image 61
lucian.pantelimon Avatar answered Sep 24 '22 09:09

lucian.pantelimon