Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jp/ac/tsukuba/openjava/SunJavaCompiler.java


1   /*
2    * SunJavaCompiler.java
3    * Workaround for Runtime.exec() environment handling incompatibility..
4    *
5    * A work based on jp.ac.tsukuba.openjava.SunJavaCompiler
6    *
7    * Apr 16, 1999  Michiaki Tatsubori (mt@is.tsukuba.ac.jp)
8    * Oct  1, 1999  Shiro Kawai (shiro@squareusa.com)
9    * Nov 22, 1999  Michiaki Tatsubori
10   */
11  package jp.ac.tsukuba.openjava;
12  
13  import java.io.BufferedInputStream;
14  import java.io.InputStream;
15  
16  import openjava.ojc.JavaCompiler;
17  
18  /**
19   * The class <code>SunJavaCompiler</code> is an adapter for Sun's javac.
20   *
21   * Message-Id: 19990930154627G.shiro@squareusa.com
22   * <p>
23   * I tried OpenJava1.0a1 on my IRIX box w/ SGI's JDK1.2
24   * and had a problem to run ojc.  Somehow, Runtime.exec()
25   * didn't pass all the environment variables to the invoked
26   * process (more specifically, it only passed TZ).
27   * Consequently the CLASSPATH env was not passed to javac kicked
28   * by JP.ac.tsukuba.openjava.SunJavaCompiler.complie(), which
29   * prevented ojc from finishing compilation.
30   * <p>
31   * So far I couldn't find exact specification about how the
32   * environment variables should be treated in Java specification
33   * and API documents.  I guess it may depend on platforms.
34   * <p>
35   * We avoided the problem by explicitly passing CLASSPATH to
36   * the subprocess my modifying SunJavaCompiler class, but wondering
37   * if there'd be a better way to handle it...
38   */
39  public class SunJavaCompiler implements JavaCompiler {
40    public static void main(String[] args) {
41      new SunJavaCompiler().compile(args);
42    }
43  
44    public void compile(String[] args) {
45      Runtime runtime = Runtime.getRuntime();
46      try {
47        String classpath =
48          "CLASSPATH=" + System.getProperty("java.class.path");
49        String[] envp = new String[1];
50        envp[0] = classpath;
51        Process p = runtime.exec("javac " + strs2str(args), envp);
52        InputStream in = new BufferedInputStream(p.getErrorStream());
53        byte[] buf = new byte[1024];
54        for (int len = in.read(buf); len != -1; len = in.read(buf)) {
55          System.err.write(buf, 0, len);
56        }
57        p.waitFor();
58      } catch (Exception e) {
59        e.printStackTrace();
60      }
61    }
62  
63    private static String strs2str(String[] strs) {
64      StringBuffer buf = new StringBuffer();
65      for (int i = 0; i < strs.length; ++i) {
66        buf.append(strs[i]).append(" ");
67      }
68      return buf.toString();
69    }
70  
71  }