static void addTestClassPath(JPFClassLoader cl, String[] testPathElements) {
   if (testPathElements != null) {
     for (String pe : testPathElements) {
       try {
         cl.addURL(FileUtils.getURL(pe));
       } catch (Throwable x) {
         error("malformed test_classpath URL: " + pe);
       }
     }
   }
 }
  static Class<?> loadTestClass(JPFClassLoader cl, Class<?> testJpfCls, String testClsName) {
    try {
      Class<?> testCls = cl.loadClass(testClsName);
      if (testJpfCls.isAssignableFrom(testCls)) {
        if (!Modifier.isAbstract(testCls.getModifiers())) {
          return testCls;
        }
      }

      return null;

    } catch (NoClassDefFoundError ncfx) {
      error("class did not resolve: " + ncfx.getMessage());
      return null;

    } catch (ClassNotFoundException cnfx) {
      error("class not found " + cnfx.getMessage() + ", check test_classpath in jpf.properties");
      return null;
    }
  }
  public static void main(String[] args) {
    int options = getOptions(args);

    if (isOptionEnabled(HELP, options)) {
      showUsage();
      return;
    }

    if (isOptionEnabled(LOG, options)) {
      Config.enableLogging(true);
    }

    config = new Config(args);

    if (isOptionEnabled(SHOW, options)) {
      config.printEntries();
    }

    args = removeConfigArgs(args);
    String testClsName = getTestClassName(args);
    String[] testArgs = getTestArgs(args);

    String[] testPathElements = getTestPathElements(config);
    JPFClassLoader cl = config.initClassLoader(RunTest.class.getClassLoader());
    addTestClassPath(cl, testPathElements);

    Class<?> testJpfCls = null;
    try {
      testJpfCls = cl.loadClass(TESTJPF_CLS);

      if (isOptionEnabled(QUIET, options)) {
        Field f = testJpfCls.getDeclaredField("quiet");
        f.setAccessible(true);
        f.setBoolean(null, true);
      }

    } catch (NoClassDefFoundError ncfx) {
      error("class did not resolve: " + ncfx.getMessage());
      return;
    } catch (ClassNotFoundException cnfx) {
      error("class not found " + cnfx.getMessage() + ", check native_classpath in jpf.properties");
      return;

      // we let pass this for now since it only means the quiet option is
      // not going to work
    } catch (NoSuchFieldException ex) {
    } catch (IllegalAccessException ex) {
    }

    List<Class<?>> testClasses = getTestClasses(cl, testJpfCls, testPathElements, testClsName);
    if (testClasses.isEmpty()) {
      System.out.println("no test classes found");
      return;
    }

    int nTested = 0;
    int nPass = 0;

    for (Class<?> testCls : testClasses) {
      nTested++;

      try {
        try {
          try { // check if there is a main(String[]) method
            Method mainEntry = testCls.getDeclaredMethod("main", String[].class);
            mainEntry.invoke(null, (Object) testArgs);

          } catch (NoSuchMethodException x) { // no main(String[]),
            // call
            // TestJPF.runTests(testCls,args)
            // directly
            Method mainEntry =
                testJpfCls.getDeclaredMethod("runTests", Class.class, String[].class);
            mainEntry.invoke(null, new Object[] {testCls, testArgs});
          }

          nPass++;

        } catch (NoSuchMethodException x) {
          error("no suitable main() or runTests() in " + testCls.getName());
        } catch (IllegalAccessException iax) {
          error(iax.getMessage());
        }

      } catch (NoClassDefFoundError ncfx) {
        error("class did not resolve: " + ncfx.getMessage());
      } catch (InvocationTargetException ix) {
        Throwable cause = ix.getCause();
        if (cause instanceof Failed) {
          // no need to report - the test did run and reported why it
          // failed
          System.exit(1);
        } else {
          error(ix.getCause().getMessage());
        }
      }
    }

    System.out.println();
    System.out.printf("tested classes: %d, passed: %d\n", nTested, nPass);
  }