You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Bootstrap.java 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // -------------------------------------------------------------------------------
  2. // Copyright (c)2000 Apache Software Foundation
  3. // -------------------------------------------------------------------------------
  4. import java.io.*;
  5. import java.util.*;
  6. import java.util.zip.*;
  7. /**
  8. * Quick and dirty single class bootstrap utility for getting Ant off
  9. * the ground when in need. To use, compile this file in the directory
  10. * where the source code is in the repository, then execute it. That's
  11. * it.<p>
  12. *
  13. * No pretense is made that this is an elegant peice of code. This code
  14. * only exists to do a ground zero build of Ant. Any other building of
  15. * Ant should be done with itself whenever possible.
  16. *
  17. * @author James Duncan Davidson (duncan@apache.org)
  18. * @author Conor MacNeill (conor@m64.com)
  19. */
  20. public class Bootstrap {
  21. /**
  22. * Command line entry point. This is the first part of the bootstrap
  23. * where we go and set up the environment and generally do what is
  24. * necessary to set up for Bootstrapping.
  25. */
  26. public static void main(String[] args) throws Exception {
  27. String[] command;
  28. String classpath = null;
  29. // check to see if we have a compiler on the classpath. Right now
  30. // we're just checking for the old compiler, but will want to check
  31. // for the new compiler and use it if it exists. Later.
  32. try {
  33. Class clazz = Class.forName("sun.tools.javac.Main");
  34. } catch (ClassNotFoundException cnfe) {
  35. String javaHome = System.getProperty("java.home");
  36. if (javaHome.endsWith("jre")) {
  37. javaHome = javaHome.substring(0, javaHome.length() - 4);
  38. }
  39. // XXX should check if this exists and bail out if it doesn't
  40. classpath = javaHome + "/lib/tools.jar" + File.pathSeparator + ".";
  41. }
  42. // XXX really should check to see if compiling the bootstrap is necessary. :)
  43. System.out.println("Compiling Bootstrap2");
  44. if (classpath == null) {
  45. command = new String[] {"javac", "./Bootstrap2.java"};
  46. } else {
  47. command = new String[] {"javac", "-classpath", classpath,
  48. "./Bootstrap2.java"};
  49. }
  50. runCommand(command);
  51. System.out.println("Running Bootstrap2");
  52. if (classpath == null) {
  53. command = new String[] {"java", "Bootstrap2"};
  54. } else {
  55. command = new String[] {"java", "-cp", classpath, "Bootstrap2"};
  56. }
  57. runCommand(command, args);
  58. }
  59. /**
  60. * Utility method for execing processes
  61. */
  62. static void runCommand(String[] command) throws IOException {
  63. System.out.print("Exec'ing: ");
  64. for (int i = 0; i < command.length; i++) {
  65. System.out.print(command[i] + " ");
  66. }
  67. System.out.println();
  68. Runtime runtime = Runtime.getRuntime();
  69. Process process = runtime.exec(command);
  70. // echo output from process
  71. InputStream in = process.getInputStream();
  72. byte[] buf = new byte[80];
  73. int count = 0;
  74. count = in.read(buf, 0, buf.length);
  75. while (count != -1) {
  76. System.out.write(buf, 0, count);
  77. count = in.read(buf, 0, buf.length);
  78. }
  79. in = process.getErrorStream();
  80. count = in.read(buf, 0, buf.length);
  81. if (count > 0) {
  82. System.out.println();
  83. System.out.println("Error Stream Output:");
  84. while (count != -1) {
  85. System.out.write(buf, 0, count);
  86. count = in.read(buf, 0, buf.length);
  87. }
  88. }
  89. }
  90. /**
  91. * Utility method for running processes that let some additional args
  92. * be specified.
  93. */
  94. static void runCommand(String[] command, String[] addtlArgs) throws IOException {
  95. String[] newCommand = new String[command.length + addtlArgs.length];
  96. for (int i = 0; i < command.length; i++) {
  97. newCommand[i] = command[i];
  98. }
  99. for (int i = 0; i < addtlArgs.length; i++) {
  100. newCommand[command.length + i] = addtlArgs[i];
  101. }
  102. runCommand(newCommand);
  103. }
  104. }