• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems with Runtime.exec()

 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ">" and "<" redirection aren't handled by the program you're executing; they're normally handled by the shell (CMD.EXE) that executes the commands you type, or batch files. TO use shell redirection this way, you need to execute the shell, and get it to execute your command. Your args array should look something like

"cmd.exe", "/c", "clientPath -l -a clientIPAddress -u userName -p userPass < inputFile > outputFile"

Although of course that last argument has to be expanded -- I used variable names where you want to use the values instead.
 
Petr Blahos
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:

"cmd.exe", "/c", "clientPath -l -a clientIPAddress -u userName -p userPass < inputFile > outputFile"


No!!! While in Java, stay as platform independent as possible.
Not only this wouldn't run too well on any Unix, this wouldn't run
on Windows95. What about:



P.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic