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.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. Runtime runtime = Runtime.getRuntime();
  64. Process process = runtime.exec(command);
  65. // echo output from process
  66. InputStream in = process.getInputStream();
  67. byte[] buf = new byte[80];
  68. int count = 0;
  69. count = in.read(buf, 0, buf.length);
  70. while (count != -1) {
  71. System.out.write(buf, 0, count);
  72. count = in.read(buf, 0, buf.length);
  73. }
  74. in = process.getErrorStream();
  75. count = in.read(buf, 0, buf.length);
  76. if (count > 0) {
  77. System.out.println();
  78. System.out.println("Error Stream Output:");
  79. while (count != -1) {
  80. System.out.write(buf, 0, count);
  81. count = in.read(buf, 0, buf.length);
  82. }
  83. }
  84. }
  85. /**
  86. * Utility method for running processes that let some additional args
  87. * be specified.
  88. */
  89. static void runCommand(String[] command, String[] addtlArgs) throws IOException {
  90. String[] newCommand = new String[command.length + addtlArgs.length];
  91. for (int i = 0; i < command.length; i++) {
  92. newCommand[i] = command[i];
  93. }
  94. for (int i = 0; i < addtlArgs.length; i++) {
  95. newCommand[command.length + i] = addtlArgs[i];
  96. }
  97. runCommand(newCommand);
  98. }
  99. }