2013-03-03 32 views
0

我已经能够从java程序中编译python程序。我正在捕获python程序中发现的错误,并在控制台中打印出来。 我一次不会收到.py文件中的所有错误。 我必须纠正先前的错误并再次运行以获得下一个错误。下面是我在控制台中得到的样本: enter image description here在Eclipse中编译Python文件返回的Disect日志

我只需要捕获行号和语法错误,例如:pint。

这里是我的方案的样本,其中log是字符串,它包含有关该错误的详细信息发现:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 

public class Python_Compiler { 
    public static void main(String args[]){ 

     String ret = compile(); 
     System.out.println(ret); 

    } 
     public static String compile() 
     { 
      String log=""; 
      String myDirectory = "C:\\"; 
      try { 
       String s= null; 
       //change this string to your compilers location 
       Process p = Runtime.getRuntime().exec("cmd /C python Hello.py", null, new java.io.File(myDirectory)); 

      BufferedReader stdError = new BufferedReader(new 
        InputStreamReader(p.getErrorStream())); 
      boolean error=false; 

      log+=""; 
      while ((s = stdError.readLine()) != null) { 
       log+=s; 
       error=true; 
       log+="\n"; 

      }if(error==false) log+="Compilation Successful !!!"; 


     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
      return log; 
     } 


     public int runProgram() 
     { 
      int ret = -1; 
      try 
      {    
       Runtime rt = Runtime.getRuntime(); 
       Process proc = rt.exec("cmd.exe /c start a.exe"); 
       proc.waitFor(); 
       ret = proc.exitValue(); 
      } catch (Throwable t) 
       { 
       t.printStackTrace(); 
       return ret; 
       } 
      return ret;      
     }} 

可以在只提取的行号和语法错误和任何人的帮助有它保存在一个数组?

回答

0

所以你想捕捉行号是3和错误是主要的。