summaryrefslogtreecommitdiff
path: root/src/main/java/jp/ac/tsukuba/openjava/SunJavaCompiler.java
blob: ef9ba903177586535259ee9c2a5f42742a323433 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * SunJavaCompiler.java
 * Workaround for Runtime.exec() environment handling incompatibility..
 *
 * A work based on jp.ac.tsukuba.openjava.SunJavaCompiler
 *
 * Apr 16, 1999  Michiaki Tatsubori (mt@is.tsukuba.ac.jp)
 * Oct  1, 1999  Shiro Kawai (shiro@squareusa.com)
 * Nov 22, 1999  Michiaki Tatsubori
 */
package jp.ac.tsukuba.openjava;

import java.io.BufferedInputStream;
import java.io.InputStream;

import io.devnulllabs.openjava.ojc.JavaCompiler;

/**
 * The class <code>SunJavaCompiler</code> is an adapter for Sun's javac.
 *
 * Message-Id: 19990930154627G.shiro@squareusa.com
 * <p>
 * I tried OpenJava1.0a1 on my IRIX box w/ SGI's JDK1.2
 * and had a problem to run ojc.  Somehow, Runtime.exec()
 * didn't pass all the environment variables to the invoked
 * process (more specifically, it only passed TZ).
 * Consequently the CLASSPATH env was not passed to javac kicked
 * by JP.ac.tsukuba.openjava.SunJavaCompiler.complie(), which
 * prevented ojc from finishing compilation.
 * <p>
 * So far I couldn't find exact specification about how the
 * environment variables should be treated in Java specification
 * and API documents.  I guess it may depend on platforms.
 * <p>
 * We avoided the problem by explicitly passing CLASSPATH to
 * the subprocess my modifying SunJavaCompiler class, but wondering
 * if there'd be a better way to handle it...
 */
public class SunJavaCompiler implements JavaCompiler {
    public static void main(String[] args) {
        new SunJavaCompiler().compile(args);
    }

    public void compile(String[] args) {
        Runtime runtime = Runtime.getRuntime();
        try {
            String classpath =
                "CLASSPATH=" + System.getProperty("java.class.path");
            String[] envp = new String[1];
            envp[0] = classpath;
            Process p = runtime.exec("javac " + strs2str(args), envp);
            InputStream in = new BufferedInputStream(p.getErrorStream());
            byte[] buf = new byte[1024];
            for (int len = in.read(buf); len != -1; len = in.read(buf)) {
                System.err.write(buf, 0, len);
            }
            p.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String strs2str(String[] strs) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < strs.length; ++i) {
            buf.append(strs[i]).append(" ");
        }
        return buf.toString();
    }

}