File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Problems with Runtime.exec()

Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2591

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);

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???

- Manish
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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:


Get a better web browser:<br /><a href="" target="_blank" rel="nofollow"></a>
I agree. Here's the link:
subject: Problems with Runtime.exec()
It's not a secret anymore!