1 18 19 package org.apache.tools.ant.taskdefs.compilers; 20 21 import java.io.OutputStream ; 22 import java.lang.reflect.Constructor ; 23 import java.lang.reflect.Method ; 24 import org.apache.tools.ant.BuildException; 25 import org.apache.tools.ant.Project; 26 import org.apache.tools.ant.util.JavaEnvUtils; 27 import org.apache.tools.ant.util.FileUtils; 28 import org.apache.tools.ant.taskdefs.LogOutputStream; 29 import org.apache.tools.ant.types.Commandline; 30 31 38 public class Javac12 extends DefaultCompilerAdapter { 39 protected static final String CLASSIC_COMPILER_CLASSNAME = "sun.tools.javac.Main"; 40 41 46 public boolean execute() throws BuildException { 47 attributes.log("Using classic compiler", Project.MSG_VERBOSE); 48 Commandline cmd = setupJavacCommand(true); 49 50 OutputStream logstr = new LogOutputStream(attributes, Project.MSG_WARN); 51 try { 52 Class c = Class.forName(CLASSIC_COMPILER_CLASSNAME); 55 Constructor cons = 56 c.getConstructor(new Class [] {OutputStream .class, 57 String .class}); 58 Object compiler 59 = cons.newInstance(new Object [] {logstr, "javac"}); 60 61 Method compile = c.getMethod("compile", 63 new Class [] {String [].class}); 64 Boolean ok = 65 (Boolean ) compile.invoke(compiler, 66 new Object [] {cmd.getArguments()}); 67 return ok.booleanValue(); 68 } catch (ClassNotFoundException ex) { 69 throw new BuildException("Cannot use classic compiler , as it is " 70 + "not available. \n" 71 + " A common solution is " 72 + "to set the environment variable" 73 + " JAVA_HOME to your jdk directory.\n" 74 + "It is currently set to \"" 75 + JavaEnvUtils.getJavaHome() 76 + "\"", 77 location); 78 } catch (Exception ex) { 79 if (ex instanceof BuildException) { 80 throw (BuildException) ex; 81 } else { 82 throw new BuildException("Error starting classic compiler: ", 83 ex, location); 84 } 85 } finally { 86 FileUtils.close(logstr); 87 } 88 } 89 } 90 | Popular Tags |