Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime classes exec methods parameters defination

 
Abhi Handoo
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help me with the example of exec(String cmd[ ],Strin env,File) method of Runtime Class. I am confused if I could pass to commands in the first parameter and will they execute and would like to know about its other parameters also.

Please Help...
 
Tony Docherty
Bartender
Posts: 2988
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you're unclear on how a method/class works always read the API docs they are full of useful information. The following snippet from the docs explains the 3 parameters and answers your question:

cmdarray - array containing the command to call and its arguments.

envp - array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process.

dir - the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process.

The first parameter is a command and a list of arguments the command takes so unfortunately no you can't pass multiple commands in.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Docherty:
The first parameter is a command and a list of arguments the command takes so unfortunately no you can't pass multiple commands in.[/QB]


Not really true. One can use Runtime.exec() to invoke cmd.exe on Windows (or sh on Linux/Unix) and then pass the commands a line at a time though the process stdin stream.

On Linux/Unix, one can also pass the list of commands separated by a semi-colon using a command along the lines of

String[] command =
{
"sh", // Invoke the shell
"-c", // Make the shell execute the following commands
"cd /home/fred; ls; pwd; df -k", // The commands separated by semi-colons
};
 
Tony Docherty
Bartender
Posts: 2988
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really true. One can use Runtime.exec() to invoke cmd.exe on Windows (or sh on Linux/Unix) and then pass the commands a line at a time though the process stdin stream.
That's a very good point but my comment was in regard to passing multiple commands in via the first parameter which you can't do.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic