• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Runtime classes exec methods parameters defination

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic