wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Runtime classes exec methods parameters defination Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Runtime classes exec methods parameters defination" Watch "Runtime classes exec methods parameters defination" New topic
Author

Runtime classes exec methods parameters defination

Abhi Handoo
Greenhorn

Joined: Aug 23, 2007
Posts: 1
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

Joined: Aug 07, 2007
Posts: 2166
    
  47
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

Joined: Sep 07, 2004
Posts: 781

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
};


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2166
    
  47
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Runtime classes exec methods parameters defination
 
Similar Threads
Daemon/Thread Process
Changing System Time
Compile a java program from another program
select/insert
Server Start (SQL2000)