Ejemplo n.º 1
0
  protected static Class tryLoadMainClass(String jarFileName, String fileClassName)
      throws Exception {
    try {
      JarClassLoader loader;
      try {
        loader = new JarClassLoader(new URL("file:///" + jarFileName));
      } catch (MalformedURLException exc) {
        throw new Exception("MalformedURLException: " + exc.getMessage());
      }

      String className = fileClassName;
      String mainClassName = loader.getMainClassName();
      // ..
      int pos = className.indexOf(".");
      if (pos != -1) {
        className = className.substring(0, pos);
      } else {
        throw new Exception(
            "Incorrect fullname class file (must be ex: packagename/classname.class)");
      }
      // ..
      className = className.replace("/", ".");
      // ..
      Class clss = null;
      if (className.equalsIgnoreCase(mainClassName)) clss = loader.loadClass(mainClassName);

      return clss;
    } catch (Exception exc) {
      throw new Exception("loadMainClass: " + exc.getMessage());
    }
  }
Ejemplo n.º 2
0
  // this function handles the javac singleton
  protected void callJavac(String[] arguments, boolean verbose) throws FakeException {
    synchronized (this) {
      try {
        if (javac == null) {
          discoverJavac();
          JarClassLoader loader = (JarClassLoader) getClassLoader(toolsPath);
          String className = "com.sun.tools.javac.Main";
          Class<?> main = loader.forceLoadClass(className);
          Class<?>[] argsType = new Class[] {arguments.getClass(), PrintWriter.class};
          javac = main.getMethod("compile", argsType);
        }

        Object result = javac.invoke(null, new Object[] {arguments, new PrintWriter(err)});
        if (!result.equals(new Integer(0))) {
          FakeException e = new FakeException("Compile error");
          e.printStackTrace();
          throw e;
        }
        return;
      } catch (FakeException e) {
        /* was compile error */
        throw e;
      } catch (Exception e) {
        e.printStackTrace(err);
        err.println(
            "Could not find javac "
                + e
                + " (tools path = "
                + toolsPath
                + "), "
                + "falling back to system javac");
      }
    }

    // fall back to calling javac
    String[] newArguments = new String[arguments.length + 1];
    newArguments[0] = "javac";
    System.arraycopy(arguments, 0, newArguments, 1, arguments.length);
    try {
      execute(newArguments, new File("."), verbose);
    } catch (Exception e) {
      throw new FakeException("Could not even fall back " + " to javac in the PATH");
    }
  }
Ejemplo n.º 3
0
  public static void run(String args[]) throws Exception {

    args = processArgs(args);

    initializeLogging();

    // Is the main class specified on the command line?  If so, boot it.
    // Otherwise, read the main class out of the manifest.
    String mainClass = null;

    initializeProperties();
    // Reinitialze Logging (property file could have other loglevel set)
    initializeLogging();

    if (Boolean.valueOf(System.getProperty(P_SHOW_PROPERTIES, "false")).booleanValue()) {
      // What are the system properties.
      Properties props = System.getProperties();
      String keys[] = props.keySet().toArray(new String[0]);
      Arrays.sort(keys);

      for (int i = 0; i < keys.length; i++) {
        String key = keys[i];
        System.out.println(key + "=" + props.get(key));
      }
    }

    // Process developer properties:
    if (mainClass == null) {
      mainClass = System.getProperty(P_MAIN_CLASS);
    }

    if (mainJar == null) {
      String app = System.getProperty(P_MAIN_APP);
      if (app != null) {
        mainJar = "main/" + app + ".jar";
      } else {
        mainJar = System.getProperty(P_MAIN_JAR, MAIN_JAR);
      }
    }

    // Pick some things out of the top-level JAR file.
    String jar = getMyJarPath();
    JarFile jarFile = new JarFile(jar);
    Manifest manifest = jarFile.getManifest();
    Attributes attributes = manifest.getMainAttributes();
    String bootLoaderName = attributes.getValue(ONE_JAR_CLASSLOADER);

    if (mainJar == null) {
      mainJar = attributes.getValue(ONE_JAR_DEFAULT_MAIN_JAR);
    }

    String mainargs = attributes.getValue(ONE_JAR_MAIN_ARGS);
    if (mainargs != null && args.length == 0) {
      // Replace the args with built-in.  Support escaped whitespace.
      args = mainargs.split("[^\\\\]\\s");
      for (int i = 0; i < args.length; i++) {
        args[i] = args[i].replaceAll("\\\\(\\s)", "$1");
      }
    }

    // If no main-class specified, check the manifest of the main jar for
    // a Boot-Class attribute.
    if (mainClass == null) {
      mainClass = attributes.getValue(ONE_JAR_MAIN_CLASS);
      if (mainClass == null) {
        mainClass = attributes.getValue(BOOT_CLASS);
        if (mainClass != null) {
          LOGGER.warning(
              "The manifest attribute "
                  + BOOT_CLASS
                  + " is deprecated in favor of the attribute "
                  + ONE_JAR_MAIN_CLASS);
        }
      }
    }

    if (mainClass == null) {
      // Still don't have one (default).  One final try: look for a jar file in a
      // main directory.  There should be only one, and it's manifest
      // Main-Class attribute is the main class.  The JarClassLoader will take
      // care of finding it.
      InputStream is = Boot.class.getResourceAsStream("/" + mainJar);
      if (is != null) {
        JarInputStream jis = new JarInputStream(is);
        Manifest mainmanifest = jis.getManifest();
        jis.close();
        mainClass = mainmanifest.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
      } else {
        // There is no main jar. Info unless mainJar is empty string.
        // The load(mainClass) will scan for main jars anyway.
        if (mainJar != null && !mainJar.isEmpty()) {
          LOGGER.info(
              "Unable to locate main jar '" + mainJar + "' in the JAR file " + getMyJarPath());
        }
      }
    }

    // Do we need to create a wrapping classloader?  Check for the
    // presence of a "wrap" directory at the top of the jar file.
    URL url = Boot.class.getResource(WRAP_JAR);

    if (url != null) {
      // Wrap class loaders.
      final JarClassLoader bootLoader = getBootLoader(bootLoaderName);
      bootLoader.load(null);

      // Read the "Wrap-Class-Loader" property from the wraploader jar file.
      // This is the class to use as a wrapping class-loader.
      InputStream is = Boot.class.getResourceAsStream(WRAP_JAR);
      if (is != null) {
        JarInputStream jis = new JarInputStream(is);
        final String wrapLoader = jis.getManifest().getMainAttributes().getValue(WRAP_CLASS_LOADER);
        jis.close();
        if (wrapLoader == null) {
          LOGGER.warning(
              url
                  + " did not contain a "
                  + WRAP_CLASS_LOADER
                  + " attribute, unable to load wrapping classloader");
        } else {
          LOGGER.info("using " + wrapLoader);
          JarClassLoader wrapped = getWrapLoader(bootLoader, wrapLoader);
          if (wrapped == null) {
            LOGGER.warning(
                "Unable to instantiate "
                    + wrapLoader
                    + " from "
                    + WRAP_DIR
                    + ": using default JarClassLoader");
            wrapped = getBootLoader(null);
          }
          setClassLoader(wrapped);
        }
      }
    } else {
      setClassLoader(getBootLoader(bootLoaderName, Boot.class.getClassLoader()));
      LOGGER.info("using JarClassLoader: " + getClassLoader().getClass().getName());
    }

    // Allow injection of the URL factory.
    String urlfactory = attributes.getValue(ONE_JAR_URL_FACTORY);
    if (urlfactory != null) {
      loader.setURLFactory(urlfactory);
    }

    mainClass = loader.load(mainClass);

    if (mainClass == null && !loader.isExpanded())
      throw new Exception(
          getMyJarName()
              + " main class was not found (fix: add main/main.jar with a Main-Class manifest attribute, or specify -D"
              + P_MAIN_CLASS
              + "=<your.class.name>), or use "
              + ONE_JAR_MAIN_CLASS
              + " in the manifest");

    if (mainClass != null) {
      // Guard against the main.jar pointing back to this
      // class, and causing an infinite recursion.
      String bootClass = Boot.class.getName();
      if (bootClass.equals(mainClass))
        throw new Exception(
            getMyJarName()
                + " main class ("
                + mainClass
                + ") would cause infinite recursion: check main.jar/META-INF/MANIFEST.MF/Main-Class attribute: "
                + mainClass);

      Class cls = loader.loadClass(mainClass);

      endTime = System.currentTimeMillis();
      showTime();

      Method main = cls.getMethod("main", new Class[] {String[].class});
      main.invoke(null, new Object[] {args});
    }
  }