aspose file tools*
The moose likes Java in General and the fly likes Problems with Runtime.exec() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problems with Runtime.exec()" Watch "Problems with Runtime.exec()" New topic
Author

Problems with Runtime.exec()

Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Petr Blahos
Ranch Hand

Joined: Apr 28, 2004
Posts: 131
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.


Get a better web browser:<br /><a href="http://www.mozilla.org/products/firefox/switch.html" target="_blank" rel="nofollow">http://www.mozilla.org/products/firefox/switch.html</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems with Runtime.exec()