KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > compilers > Javac12


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.compilers;
20
21 import java.io.OutputStream JavaDoc;
22 import java.lang.reflect.Constructor JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
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 /**
32  * The implementation of the javac compiler for JDK 1.2
33  * This is primarily a cut-and-paste from the original javac task before it
34  * was refactored.
35  *
36  * @since Ant 1.3
37  */

38 public class Javac12 extends DefaultCompilerAdapter {
39     protected static final String JavaDoc CLASSIC_COMPILER_CLASSNAME = "sun.tools.javac.Main";
40
41     /**
42      * Run the compilation.
43      * @return true if the compiler ran with a zero exit result (ok)
44      * @exception BuildException if the compilation has problems.
45      */

46     public boolean execute() throws BuildException {
47         attributes.log("Using classic compiler", Project.MSG_VERBOSE);
48         Commandline cmd = setupJavacCommand(true);
49
50         OutputStream JavaDoc logstr = new LogOutputStream(attributes, Project.MSG_WARN);
51         try {
52             // Create an instance of the compiler, redirecting output to
53
// the project log
54
Class JavaDoc c = Class.forName(CLASSIC_COMPILER_CLASSNAME);
55             Constructor JavaDoc cons =
56                 c.getConstructor(new Class JavaDoc[] {OutputStream JavaDoc.class,
57                                               String JavaDoc.class});
58             Object JavaDoc compiler
59                 = cons.newInstance(new Object JavaDoc[] {logstr, "javac"});
60
61             // Call the compile() method
62
Method JavaDoc compile = c.getMethod("compile",
63                                          new Class JavaDoc [] {String JavaDoc[].class});
64             Boolean JavaDoc ok =
65                 (Boolean JavaDoc) compile.invoke(compiler,
66                                         new Object JavaDoc[] {cmd.getArguments()});
67             return ok.booleanValue();
68         } catch (ClassNotFoundException JavaDoc 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 JavaDoc 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