• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Problems with Runtime.exec()

 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic