/Users/lyon/j4p/src/net/rmi/rmiSynth/ClassCompiler.java

1    package net.rmi.rmiSynth; 
2     
3    /** 
4     * A class that generates .class files 
5     * <p/> 
6     * It currently uses the sun.tools.javac.* 
7     * classes 
8     */ 
9     
10   public class ClassCompiler { 
11    
12       /** 
13        * Invoke the javac compiler. 
14        */ 
15       public static boolean compile( 
16               String fileName, String classpath) { 
17           String args[] = new String[4]; 
18           args[0] = "-classpath"; 
19           args[1] = classpath; 
20           args[2] = "-nowarn"; 
21           args[3] = fileName; 
22    
23           //System.err.println("javac -classpath "+classpath+" -nowarn "+fileName); 
24    
25           Process p = null; 
26           try { 
27               // The prefered way to invoke javac on JDK1.2 and JDK1.1 platforms. 
28               p = Runtime.getRuntime().exec("javac " 
29                                             + 
30                                             args[0] 
31                                             + " " 
32                                             + 
33                                             args[1] 
34                                             + " " 
35                                             + 
36                                             args[2] 
37                                             + " " 
38                                             + 
39                                             args[3]); 
40    
41               return (p.waitFor() == 0) ? 
42                      true : 
43                      false; // status code 0 indicates success. 
44           } catch (Throwable th) { 
45               System.err.println("WARNING: Could not Runtime.exec(String) \"javac\" in" 
46                                  + 
47                                  " the standard way: " 
48                                  + th); 
49           } 
50    
51           return warningMessage(); 
52       } 
53    
54       static boolean warningMessage() { 
55           System.err.println(""); 
56           System.err.println( 
57                   "Check that the version of \"javac\" that you are running"); 
58           System.err.println( 
59                   "is the one supplied with Sun's JDK1.x (which includes the"); 
60           System.err.println( 
61                   "compiler classes) and not some other version of \"java\""); 
62           System.err.println( 
63                   "or JRE shipped with some other product."); 
64           System.err.println(""); 
65           return false; 
66       } 
67    
68   } 
69