K DEPENDENCIES ON STARTUP As applications increase in complexity, single distributions are becoming more likely to require classes from third party libraries in order to function. If dependencies are not satisfied, your application may fail unexpectedly with a ClassNotFoundException when the referenced classes are needed during execution. One way to check for dependencies at startup is to use the Class.forName() method to check that the class reference is known to the current class loader. Here's an example application that checks if a class from a needed package is present and, if not, exits with an informative message: public class DependencyChecker { public static void main(String[] args) { try { dependencyCheck(); System.out.println("Dependencies OK"); //continue with application startup... }catch(ExceptionInInitializerError initError) { System.out.println("Initialization failed: "+initError.getMessage()); } } private static void dependencyCheck () { load ("java.util.Arrays","java.util.*"); //demonstrates found class behavior load ("missing.package.class","Acme Class Library"); //demonstrates missing class behavior //comment the previous line to demonstrate a successful application startup } private static void load (String className, String referenceName) { try { Class.forName(className); } catch (Throwable t) { throw new ExceptionInInitializerError("Could not locate required classes - "+referenceName+" is missing."); } } } You can extend this pattern. For example, add an additional parameter to the load method to provide a URL for support or as a pointer to the referenced package. In a GUI application, you will also want to use a dialog rather than a simple console message to notify the user of the error. You'll also need to judiciously choose the classes you want to check; normally, you'll just need to include one class from each separately distributed package that's needed by your application. By checking class references from any third party or other required package with the Class.forName() method, you can ensure that your application will notify users concerning any missing components as soon as possible. ------------------------------------------
Hosted by www.Geocities.ws

1