This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Running a command on a remote system

 
Jason Thiesse
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help!! I'll post what I get to work.
 
Jason Thiesse
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic