Detect if a package is availableTag(s): Language


public class ClassUtils {

  private ClassUtils() {}

  public static boolean isAvailable(String className) {
    boolean isFound = false;
    try {
       Class.forName(className, false, null);
       isFound = true;
    }
    catch (ClassNotFoundException e) {
       isFound = false;
    }
    return isFound;
  }

  public static boolean isJava3dAvailable() {
    return ClassUtils.isAvailable("javax.media.j3d.View");
  }

  public static void  main(String args[]) {
    System.out.println ("Swing " +
         (ClassUtils.isAvailable("javax.swing.JComponent")?"present":"absent"));
    System.out.println ("JAI " +
         (ClassUtils.isAvailable("javax.media.jai.ImageJAI")?"present":"absent"));
    System.out.println ("SAX " +
         (ClassUtils.isAvailable("org.xml.sax.XMLReader")?"present":"absent"));
    System.out.println ("ImaginaryClass " +
         (ClassUtils.isAvailable("imaginary.ImaginaryClass")?"present":"absent"));
    System.out.println ("Java3d " +
         (ClassUtils.isJava3dAvailable()?"present":"absent"));
      /*
      output :
          Swing present
          JAI absent
          SAX present
          ImaginaryClass absent
          Java3d absent
      */
  }
}

blog comments powered by Disqus