posted by 희정냥★ 2008. 11. 4. 17:40
import java.io.IOException;

import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

import com.sun.tools.javac.api.JavacTool;

public class TestCompile {
    public static void main(String[] args) {
      
        String filename = "c:/Cutewebi.java";
        boolean compRes = false;

  try {
  
   JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
      if (compiler == null)
           compiler = JavacTool.create();

   StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); 
   Iterable compilationUnits2 = fileManager.getJavaFileObjects(filename);
   compRes = compiler.getTask(null, fileManager, null, null, null, compilationUnits2).call();
   fileManager.close();
         if (compRes) {
             System.out.println ("Compilation was successful");
         } else {
             System.out.println ("Compilation failed");
         }
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

    }
} 

jdk 폴더에 있는 tools.jar를 라이브러리로 추가해야 합니다.

댓글을 달아 주세요