1. /*
  2. * Copyright 2001-2002,2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.compilers;
  18. import java.io.IOException;
  19. import java.io.OutputStream;
  20. import java.lang.reflect.Constructor;
  21. import java.lang.reflect.Method;
  22. import org.apache.tools.ant.BuildException;
  23. import org.apache.tools.ant.Project;
  24. import org.apache.tools.ant.taskdefs.LogOutputStream;
  25. import org.apache.tools.ant.types.Commandline;
  26. /**
  27. * The implementation of the javac compiler for JDK 1.2
  28. * This is primarily a cut-and-paste from the original javac task before it
  29. * was refactored.
  30. *
  31. * @since Ant 1.3
  32. */
  33. public class Javac12 extends DefaultCompilerAdapter {
  34. /**
  35. * Run the compilation.
  36. *
  37. * @exception BuildException if the compilation has problems.
  38. */
  39. public boolean execute() throws BuildException {
  40. attributes.log("Using classic compiler", Project.MSG_VERBOSE);
  41. Commandline cmd = setupJavacCommand(true);
  42. OutputStream logstr = new LogOutputStream(attributes, Project.MSG_WARN);
  43. try {
  44. // Create an instance of the compiler, redirecting output to
  45. // the project log
  46. Class c = Class.forName("sun.tools.javac.Main");
  47. Constructor cons =
  48. c.getConstructor(new Class[] {OutputStream.class,
  49. String.class});
  50. Object compiler
  51. = cons.newInstance(new Object[] {logstr, "javac"});
  52. // Call the compile() method
  53. Method compile = c.getMethod("compile",
  54. new Class [] {String[].class});
  55. Boolean ok =
  56. (Boolean) compile.invoke(compiler,
  57. new Object[] {cmd.getArguments()});
  58. return ok.booleanValue();
  59. } catch (ClassNotFoundException ex) {
  60. throw new BuildException("Cannot use classic compiler, as it is "
  61. + "not available. A common solution is "
  62. + "to set the environment variable"
  63. + " JAVA_HOME to your jdk directory.",
  64. location);
  65. } catch (Exception ex) {
  66. if (ex instanceof BuildException) {
  67. throw (BuildException) ex;
  68. } else {
  69. throw new BuildException("Error starting classic compiler: ",
  70. ex, location);
  71. }
  72. } finally {
  73. try {
  74. logstr.close();
  75. } catch (IOException e) {
  76. // plain impossible
  77. throw new BuildException(e);
  78. }
  79. }
  80. }
  81. }