【问题标题】:How to run windows console application in java code using exec() method?如何使用 exec() 方法在 java 代码中运行 Windows 控制台应用程序?
【发布时间】:2011-07-12 11:19:38
【问题描述】:

如何使用 Runtime.getRuntime().exec()?

我试试:

try {
    String[] callAndArgs = {"path\\program.exe","arg1","arg2","arg3"}
    System.out.println("before");
    Process p = Runtime.getRuntime().exec(callAndArgs)
    System.out.println("in");
    p.waitFor();
    System.out.println("after");
} catch (IOException e) {
            System.out.println("catch io: " + e.getMessage());
        } catch (InterruptedException ie) {
            System.out.println("catch ie: " + ie.getMessage());
        }

而且程序不工作。输出:

before
in
after

嗯?帮助;p

【问题讨论】:

    标签: java runtime.exec


    【解决方案1】:

    【讨论】:

    • 这是一个仅限链接的答案,如果(或何时)链接失效,它将没有用处。
    【解决方案2】:

    也许这也有帮助 (http://ubuntuforums.org/showthread.php?t=681779),

    import java.lang.* ;
    
     public class MyJavaClass
     {
     public void runCmd(String[] args)
     {
     String cmd = "/home_dir/./my_shell_script.sh" ;
     Runtime run = Runtime.getRuntime() ;
     Process pr = run.exec(cmd) ;
     pr.waitFor() ;
     BufferedReader buf = new BufferedReader( new InputStreamReader( pr.getInputStream() ) ) ;
    
     while ( ( String line ; line = buf.readLine() ) != null ) 
     {
     System.out.println(line) ;
     }
    
     }
    
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      • 2012-02-03
      • 1970-01-01
      • 2019-01-30
      相关资源
      最近更新 更多