Java SE6 transfers the Java compiler's two new methods

Author:Anonymous    Updated:2008-10-9 18:35:20

Needs to transfer the Java compiler in many Java applications in the procedure to translate and the movement. But (Java SE5 and the beforehand edition) can only transfer the Java compiler in the early edition through the tools.jar in com.sun.tools.javac package, but because tools.jar is not the standard Java storehouse, when use must establish this jar the way. But provided the standard package in Java SE6 for us to operate the Java compiler, this was the javax.tools package. Uses this package, we might not need the jar document way to increase to classpath.

  First, uses the JavaCompiler connection to translate the Java source program

  Uses Java API to translate the Java source program to have many methods, now lets us look that one kind of simple the method, carries on the translation through JavaCompiler.

  We may obtain a JavaCompiler connection example through ToolProvider kind of static method getSystemJavaCompiler.

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();


  In JavaCompiler the most core's method is run. May translate the java source program through this method. This method has 3 preset parameters and 1 varying parameter (varying parameter is a new parameter type which starts from Jave SE5 to provide, with type… argu expression). The first 3 parameters respectively use for are the java compiler provide the parameter, to obtain the Java compiler's produce information as well as to receive compiler's error message, the following varying parameter may spread to or a many Java source program document. If run translates successfully, returns 0.

int run (InputStream, OutputStream out, OutputStream err, String… arguments)


  What if the first 3 parameters spread is null, then the run method by the standard input, the output will replace, namely System., System.out and System.err. If we must translate a test.java document, and will use the stdio, the run application method is as follows:

int results = tool.run (null, null, null, “test.java”);


  Below is uses JavaCompiler the complete code:

import java.io.*;
import javax.tools.*;

public class test_compilerapi
{
 public static void main (String args[]) throws IOException
 {
  JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  int results = compiler.run (null, null, null, “test.java”);
  System.out.println ((results == 0)? “translates successfully”: “translation defeat”);
  // moves test in the procedure
  Runtime run = Runtime.getRuntime();
  Process p = run.exec (“java test”);
  BufferedInputStream in = new BufferedInputStream (p.getInputStream());
  BufferedReader br = new BufferedReader (new InputStreamReader(in));
  String s;
  while ((s = br.readLine())! = null)
   System.out.println(s);
 }
}

public class test
{
 public static void main(String[] args) throws Exception
 {
  System.out.println (“JavaCompiler tests successfully!”);
 }
}


  Translates the successful output result:

  Translates successfully

  JavaCompiler tests successfully

  Translation defeat's output result:

test.java:9: Could not find the mark
Mark: Method printlnln (java.lang. String)
Position: Kind of java.io.Pr intStream
System.out.printlnln (“JavaCompiler tests successfully!”);
^
1 mistake
Translation defeat
Previous:Discussed shallowly in Java realizes using JCOM imitates the Excel programming
Next:Uses JBuilder2007 to develop the Web application procedure
User Reviews
Site Search
Related Articles
Recommended article
AD