Java execute command line program

后端 未结 2 1288
忘了有多久
忘了有多久 2020-12-11 05:45

I have a little problem with executing a command line tool. I want to execute UnRAR.exe from WinRAR. I do it like this:

Process process = runtime.exec(\"\\\"         


        
相关标签:
2条回答
  • 2020-12-11 06:37

    Works for me. Perhaps you didn't write a newline and flush the stream?

    Process tr = Runtime.getRuntime().exec( new String[]{ "cat" } );
    Writer wr = new OutputStreamWriter( tr.getOutputStream() );
    BufferedReader rd = new BufferedReader( new InputStreamReader( tr.getInputStream() ) );
    wr.write( "hello, world\n" );
    wr.flush();
    String s = rd.readLine();
    System.out.println( s );
    

    http://ideone.com/OUGYv

    +1 to your question, java.lang.Process was what I was looking for!

    0 讨论(0)
  • 2020-12-11 06:42

    There are few methods to run process and interact with it.

    There is Process class in java. It allows to get all 3 streams of subprocess (process called by your java app).

    Process class in java help: link

    Here you can find few examples of code, that executes process in java: link

    However data from output and error streams will be available for you after process ends (your program will be "stopped" on line with .exec() method until subprocess ends).

    To interact with running process you have to use another thread.

    Using a thread to capture process output

    Interacting with another process Java

    0 讨论(0)
提交回复
热议问题