2014-04-14 38 views
-1

我有一个用C语言开发的控制台应用程序。我需要从我的java代码中调用这个程序功能。另外,我需要将一些参数(如List <>)传递给主函数。我研究了一个发现的this链接,但它是用于J Python代码的。我们有类似的C Python工具吗?从Java调用Python程序

回答

0
import java.io.IOException; 

import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 

public class SystemCall{ 

     private static Log logger = LogFactory.getLog(SystemCall.class); 

    public static int execute(String command) { 
     int result = 0; 
     if (command == null){ 
       throw new RuntimeException("harness command is null"); 
     } 
     Runtime r = Runtime.getRuntime(); 

     try { 
      Process p = r.exec(command); 
      try { 
       if (p.waitFor() != 0) { 
        result = p.exitValue(); 
       } 
      } catch (InterruptedException e) { 
       logger.error("System call interupted.", e); 
       //TODO 
      } finally { 
       //TODO 
      } 
     } catch (IOException e) { 
       logger.error("IO error in system call.", e); 
       //TODO 
     } 
     return result; 
    } 
}