This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Java in General and the fly likes Running a command on a remote system Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Running a command on a remote system" Watch "Running a command on a remote system" New topic
Author

Running a command on a remote system

Jason Thiesse
Greenhorn

Joined: Mar 28, 2001
Posts: 5
I would like to run a command on a remote system from a Java application running on another system. Namely I would like to start a Java application on the remote system. Does anybody have any idea on how to do this? Thanks for the help
Mark Savory
Ranch Hand

Joined: Feb 08, 2001
Posts: 122
In English, you need a server on the remote system that's always listening for requests to start another application. As far as I know, you'll have to write your own although there might be something out there already. A browser performs this functions when it runs applets.
Brian Nice
Ranch Hand

Joined: Nov 02, 2000
Posts: 195
You could write a java program on the server that listens on a particular socket. Your other program would send a message to over that socket to the server program. The message could be a Vector, or String (use the ObjectOuputStream class to serialize your object before sending)
An example of the message would be an ArrayList:
ArrayList[0] = classname
ArrayList[1] = parameter1
ArrayList[2] = parameter2
Now the server program will de-serialize the object sent across, break it apart into its components, and could use reflection to call the class and any of its methods (you could add the method name to another index in the array list and any parameters that that needs also)
Or if the class performs some function, then you may need to investigate the Runtime class also, which would start your java program and then wait for it to complete before accepting any more messages over the socket, otherwise you could probably launch the applications each in a separate thread.
Hopefully this makes a little sense
Brian
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
If you have a webserver on the remote system it's a simple matter to knock together a CGI to start any sort of application, java included, and return a "done it" web page. This is the kind of thing the CGI protocol was invented for.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
As much as I'm a Java zealot, there's another option. Computer A is running your first java program. It uses Runtime.exec() to invoke a script on computer A, which connects to computer B, and runs a sequence of commands there, which start the second Java program. This is an option if you'd rather not be running a Java server on the second machine.

--Mark
hershey@vaultus.com
Jason Thiesse
Greenhorn

Joined: Mar 28, 2001
Posts: 5
Thanks for all the help!! I'll post what I get to work.
Jason Thiesse
Greenhorn

Joined: Mar 28, 2001
Posts: 5
This is exactly what I'm trying to do!! How do I connect to System B? Is ther any security worries?
Thanks
Jason
Originally posted by Mark Herschberg:
As much as I'm a Java zealot, there's another option. Computer A is running your first java program. It uses Runtime.exec() to invoke a script on computer A, which connects to computer B, and runs a sequence of commands there, which start the second Java program. This is an option if you'd rather not be running a Java server on the second machine.

--Mark
hershey@vaultus.com

Mark Savory
Ranch Hand

Joined: Feb 08, 2001
Posts: 122
Mark Herschberg,
How do you connect to computer B?
The previous replys try to make it clear that you need a listner/server on computer B in order to "connect" remotely.
Jason Thiesse
Greenhorn

Joined: Mar 28, 2001
Posts: 5
I am not able to have a Java server, or any other server running on the the second machine (System B). I would like to connect to the System and exicute a command to start a server. From what I understand of RMI I would have to start the Server on System B ahead of time. Please correct me if I am wrong.
Originally posted by Mark Herschberg:
...This is an option if you'd rather not be running a Java server on the second machine.

--Mark
hershey@vaultus.com

Mark Savory
Ranch Hand

Joined: Feb 08, 2001
Posts: 122
There has to be a server running on computer B - not neccessarily one that you started. You can Telnet to the remote computer and run a script. In that case a Telnet daemon(basically a server) would have to be running on computer B.
Jason Thiesse
Greenhorn

Joined: Mar 28, 2001
Posts: 5
Thanks for the help!!
Originally posted by Mark Savory:
There has to be a server running on computer B - not neccessarily one that you started. You can Telnet to the remote computer and run a script. In that case a Telnet daemon(basically a server) would have to be running on computer B.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running a command on a remote system