MewZ
MewZ

Reputation: 121

how to excute shell script on difference machine using java

i want java code example for excute shell script on difference machine (from windows OS to Unix).Help me pls.

Sorry for my question unclear.i have 2 use case for this question :

case 1 : Different Machine

case 2 : Different OS (because first machine is windows 2003 server os and remote machine is unix)

Upvotes: 1

Views: 1463

Answers (3)

Rupeshit
Rupeshit

Reputation: 1466

   // Maximize portability by looking up the name of the command to execute
   // in a configuration file. 
   java.util.Properties config;  
   String cmd = config.getProperty("sysloadcmd");
   if (cmd != null) {
  // Execute the command; Process p represents the running command
   Process p = Runtime.getRuntime().exec(cmd);         // Start the command
  InputStream pin = p.getInputStream();               // Read bytes from it
  InputStreamReader cin = new InputStreamReader(pin); // Convert them to chars
  BufferedReader in = new BufferedReader(cin);        // Read lines of chars
  String load = in.readLine();                        // Get the command output
  in.close();                                         // Close the stream

}

Upvotes: 0

Stephen
Stephen

Reputation: 6087

You will (probably) want to use a SSH library (I believe JSch is popular) to create a ssh connection to the machine via Java code, and then simply run the script that you want to run on the machine that you are ssh'd into. This is assuming the script you want to run is on the remote machine. If it's local then I'd probably just copy it over first before running it... or re-write the script to do the ssh-ing itself.

Upvotes: 4

Riduidel
Riduidel

Reputation: 22300

Considering that kind of question, my reference is a JavaWorld article : When runtime.exec won't.

Upvotes: 1

Related Questions