Advertisement
Guest User

compilar e executar

a guest
May 14th, 2011
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. package Compilar;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.FileReader;
  8. import java.io.FileWriter;
  9. import java.io.IOException;
  10. import java.lang.reflect.Method;
  11.  
  12. import javax.tools.JavaCompiler;
  13. import javax.tools.ToolProvider;
  14.  
  15. public class Compilador implements ICompilador{
  16.  
  17.     @Override
  18.     public String compilar(String codigo, String caminhoAplicacao , String nomeArquivo){
  19.        
  20.         FileOutputStream errorStream = null;
  21.        
  22.         try {
  23.            
  24.             salvaCodigo(codigo,caminhoAplicacao + "\\arquivos\\" + nomeArquivo );
  25.             errorStream = new FileOutputStream(caminhoAplicacao+"\\erros.txt");
  26.            
  27.         } catch (FileNotFoundException e) {
  28.             return "ERRO";
  29.         } catch (IOException e) {
  30.             return "ERRO";
  31.         }
  32.        
  33.         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  34.         compiler.run(null, null, errorStream, caminhoAplicacao + "\\arquivos\\" + nomeArquivo);
  35.        
  36.         try {
  37.             return leArquivoErros(caminhoAplicacao + "\\erros.txt");
  38.         } catch (IOException e) {
  39.             return "ERRO";
  40.         }
  41.     }
  42.  
  43.     private String leArquivoErros(String caminho) throws IOException {
  44.         BufferedReader leitor = new BufferedReader(new FileReader(caminho));
  45.         String conteudo = leitor.readLine();
  46.         String aux = "";
  47.         while((aux = leitor.readLine()) != null){
  48.             conteudo += "\n" + aux;
  49.         }
  50.         leitor.close();
  51.         return conteudo;
  52.     }
  53.  
  54.     @Override
  55.     public void executar(String caminho) {
  56.         try {
  57.             Class h = Class.forName(caminho);
  58.             Method [] metodos = h.getMethods();
  59.             for(Method met : metodos){
  60.                 System.out.println(met.getName());
  61.             }
  62.         } catch (ClassNotFoundException e) {
  63.             e.printStackTrace();
  64.         } catch (IllegalArgumentException e) {
  65.             e.printStackTrace();
  66.         }
  67.     }
  68.  
  69.     public void salvaCodigo( String conteudo , String caminho ) throws IOException{
  70.         BufferedWriter saida = new BufferedWriter( new FileWriter(caminho));
  71.         saida.write(conteudo);
  72.         saida.close();
  73.     }
  74.    
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement