Problems with Runtime.exec()

M

Manish Hatwalne

I have a command line client which takes and input file and produces some
output file, along with few other arguments. I am trying to run this program
from my Java code. This is what I have done -

String args[] = {clientPath, "-l", "-a", clientIPAddress, "-u", userName,
"-p", userPass, "<", inputFile, ">", outputFile};
Process p = Runtime.getRuntime().exec(args);
p.waitFor();

But, it doesn't work (as in the output file is not created). However, if I
create a batch file with this command line and execute it from the Java
code, it works fine. What could be the problem???? I have verified that the
command is the batch file and in Java code is exactly same.

Where should I look now???

TIA,
- Manish
 
T

Thomas Fritsch

Hi Manish!

Manish said:
I have a command line client which takes and input file and produces some
output file, along with few other arguments. I am trying to run this program
from my Java code. This is what I have done -

String args[] = {clientPath, "-l", "-a", clientIPAddress, "-u", userName,
"-p", userPass, "<", inputFile, ">", outputFile};
Process p = Runtime.getRuntime().exec(args);
p.waitFor();
This does not work because the arguments concerning in/out-redirection
("<", inputFile, ">", outputFile) also get passed to your executable
(clientPath). Executables are not capable of interpreting these
arguments. This is the job of a command-shell ("CMD.EXE" or "/bin/sh",
depending on OS).
But, it doesn't work (as in the output file is not created). However, if I
create a batch file with this command line and execute it from the Java
code, it works fine. What could be the problem???? I have verified that the
command is the batch file and in Java code is exactly same.

Where should I look now???

TIA,
- Manish
The code below should work. (Note that, now you need spaces for
separating the command-string):

// On Windows:
String args[] = { "cmd", "/c", clientPath + " -l -a " + clientIPAddress
+ " -u " + userName +
" -p " + userPass + " < " inputFile + " > " + outputFile };
Process p = Runtime.getRuntime().exec(args);
.....

// On Linux/Unix:
String args[] = { "sh", "-c", clientPath + " -l -a " + clientIPAddress +
" -u " + userName +
" -p " + userPass + " < " inputFile + " > " + outputFile };
Process p = Runtime.getRuntime().exec(args);
......
 
M

Manish Hatwalne

Thanks!!! :) :)
You were right!! I should have thought of that. I didn't need the spaces
that you mentioned though!

rgds,
- Manish

Thomas Fritsch said:
Hi Manish!

Manish said:
I have a command line client which takes and input file and produces some
output file, along with few other arguments. I am trying to run this program
from my Java code. This is what I have done -

String args[] = {clientPath, "-l", "-a", clientIPAddress, "-u", userName,
"-p", userPass, "<", inputFile, ">", outputFile};
Process p = Runtime.getRuntime().exec(args);
p.waitFor();
This does not work because the arguments concerning in/out-redirection
("<", inputFile, ">", outputFile) also get passed to your executable
(clientPath). Executables are not capable of interpreting these
arguments. This is the job of a command-shell ("CMD.EXE" or "/bin/sh",
depending on OS).
But, it doesn't work (as in the output file is not created). However, if I
create a batch file with this command line and execute it from the Java
code, it works fine. What could be the problem???? I have verified that the
command is the batch file and in Java code is exactly same.

Where should I look now???

TIA,
- Manish
The code below should work. (Note that, now you need spaces for
separating the command-string):

// On Windows:
String args[] = { "cmd", "/c", clientPath + " -l -a " + clientIPAddress
+ " -u " + userName +
" -p " + userPass + " < " inputFile + " > " + outputFile };
Process p = Runtime.getRuntime().exec(args);
....

// On Linux/Unix:
String args[] = { "sh", "-c", clientPath + " -l -a " + clientIPAddress +
" -u " + userName +
" -p " + userPass + " < " inputFile + " > " + outputFile };
Process p = Runtime.getRuntime().exec(args);
.....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top