How can I run a remote program using Runtime.exec()? For example, I'd like to launch a remote program on a Linux box from Windows.
Runtime.exec() runs programs locally, just as if you'd typed the command into a command shell. On a Linux box, it's as if you'd typed the command into a local bash shell; on a Windows box, it's as if you'd typed the command into a DOS command shell. In other words, the command is interpreted locally.
Here are some pitfalls people encounter when trying to run a command remotely:
It doesn't work. That line is interpreted locally: if emotehostprogramname resolves to a valid program on a share, that program will be read from the share and run locally.
Believing a command will work in Runtime.exec() because it works in a telnet session. But a telnet session is not a local command shell, it's a window to a shell running somewhere else.
So what does work?
To run a command remotely, you need a server on a remote machine that will accept a request for some service, and then make a request from the local program. Among the ways to do this are:
Define a CGI script on a remote Web server that runs the desired program. When the local program makes an HTTP connection to the appropriate URL, the program is run.
Run an RMI server on a remote machine and request services through an RMI call from the local machine.
Run a remote execution server (shell protocol) on the remote machine and use the corresponding client on the local machine. For example:
System.exec("rsh remotemachine programname")
Run a secure shell server on the remote machine and use the corresponding client on the local machine. For example:
System.exec("ssh remotemachine programname")
- Make up your own remote execution protocol and write server and client code.
Any of these approaches requires setting up security so access is limited to authorized users and password prompts don't show up at inconvenient times. There are many options here; the the key issue is that some server must run on the remote machine and the local machine must know how to request the needed service.