• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to use the runtime exec command ?  RSS feed

 
R Ludington
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope this is the correct forum for this...it should be simple.

I am trying to use the exec command to run an external bat file.
But I dont know how to set the arguments.
I want to use the bat file plus tell it where to run.. I find this syntax:
exec(String[] cmdarray,String[] envp, File dir)
I want the command to be test.bat I dont have any environment variables and the directory to run in is C:\test.
I try the command:
Process P = Runtime.getRuntime().exec ( wcmd,envp,"c:\\test" );
where :
String wcmd[] = {"test.bat"};
String envp[] = {null};
And it just laughs at me....
Please tell me my stupid error...I cant seem to find any examples of how to do this.
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you try String envp[] = null; instead of String envp[] = {null};
 
R Ludington
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it says method exec (string,string[]) in the type runtime is not applicable for the arguments (string[], string[],string)

Does it have something to do with the last argument.. It supposed to be type File but I dont know what that means..and Im not sure how to make the directory into that type..
 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to specify absolute path of the bat file. I find the easiest way to get it to run is:

Process a = Runtime.getRuntime().exec(path+"test.bat paramters");

then Input/OutputStreams for stdin/out/err . To set an environment: and working Directory:

Process a = Runtime.getRuntime().exec(path+"test.bat paramters",env,workDir);

Hope it helps
 
R Ludington
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I defined my path as a File object (I think)... and
I had a little sucess and a little failure ... my test.bat file consists of 2 commands:
copy c:\test.bbb c:\test.ccc
copy ..\test.ccc ..\test.ddd

the first works but the relative copy does not ....so what is wrong now ??
Is my file directory defined incorrectly ?

String path = "C:\\test\\";
String envp[]= null;
File working_dir = new File(path);
String cmd_path = "C:\\test\\";
String wcmd = cmd_path + "test.bat ";
String wwcmd[] = {wcmd," test.ccc test.ddd"};

try
{
Process P = Runtime.getRuntime().exec wwcmd,envp,working_dir);
}
catch (IOException f)
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Intermediate forum..
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!