Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java execute command line program

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("\"" + unrarPath + "\"" + " x -kb -vp " + "\"" + fileName + "\"", null, f.getParentFile());

My problem is that the compressed file is password protected. If I execute the command in my console I'm asked for a password. If I let Java execute it the program just ends and never waits for a user input (the password).

I tried to write to the process outputstream but that didn't work. Is there anything I need to know about the behavior of command line programs executed in "different" environments?

EDIT: Maybe I wasn't clear enough. My question is: Is it possible to interact with a command line program with Java?

like image 274
T3rm1 Avatar asked Feb 19 '12 02:02

T3rm1


1 Answers

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!

like image 187
Potatoswatter Avatar answered Sep 29 '22 01:09

Potatoswatter