0

I have Java Code that launch process of WinSCP tool and connects to a Unix machine and then call a xxxx.exe located on the Unix machine.

The problem is that xxxx.exe accepts a parameter of a type File. So I need to upload this to the remote machine and then passed to the xxxx.exe.... that is failing and I'm trying to avoid the temporary folders as possible.

small Code

Process p = Runtime.getRuntime().exec("rTool\\WinSCP.com /script=folder\\code.txt < C:\\FILESTOUPLOADS\\upload1.txt" );

The login information goes in code.txt as supported by WinSCP.com

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Gerard
  • 65
  • 1
  • 6
  • What is the right syntex to uplad a file and pass it as parameter ? ie in the rTool\\WinSCP.com /script=folder\\code.txt < C:\\FILESTOUPLOADS\\upload1.txt – Gerard Aug 22 '12 at 14:45
  • 1
    Would it not be more intersting to search for a good way to transfer your file to the server, instead of calling an external program? – W. Goeman Aug 22 '12 at 14:46
  • 3
    .exe on linux machine ?? – Amit Deshpande Aug 22 '12 at 14:47
  • The single line invocation of `Runtime.exec()` rarely works. Follow the recommendations of the article linked from the [Runtime.exec info. page](http://stackoverflow.com/tags/runtime.exec/info) before wondering too much why it breaks. Also, use a `ProcessBuilder` for 1.5+, and break the arguments into a `String[]` – Andrew Thompson Aug 22 '12 at 14:50

2 Answers2

1

file redirection (i.e. the "<" symbol) is handled my the command processor, which Runtime.exec() does not use. As mentioned in comments already, first use the String[] version of exec so that you don't have issues with command parsing. second, you need to invoke the command processor to handle the file redirection (e.g. using "cmd.exe /k"), or handle it yourself in java.

Community
  • 1
  • 1
jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • thank you for your replies .. but i still want the execution to be done in the remote machine . Im still need to use this extrnal tool winSCP because it allows me to have the login remotely .. – Gerard Aug 23 '12 at 07:12
  • jtahlborn: I've done this way it's woks for passing the file as parameter after transfer the file via Socket. but also i need to pass another parameter how the syntax of ProcessBuilder will be in tow parameter one of them is File and the other is string – Gerard Aug 23 '12 at 14:03
  • @Gerard - add more elements to your String[]? – jtahlborn Aug 23 '12 at 15:21
0

Why not use ProcessBuilder to change working directory and set path of the file from that directory

public ProcessBuilder directory(File directory)Sets this process builder's working directory. Subprocesses subsequently started by this object's start() method will use this as their working directory. The argument may be null -- this means to use the working directory of the current Java process, usually the directory named by the system property user.dir, as the working directory of the child process.

Parameters: directory - The new working directory Returns: This process builder

Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72