Solved

Execute unix command remotely ?

Posted on 2007-11-14
8
1,582 Views
Last Modified: 2013-12-29
Hello,

is it possible in Java to call a Unix command from one server to another ? Without RMI ?

Thank you.
0
Comment
Question by:matthew016
8 Comments
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 20280313
Look at here it might help you...
http://www.computing.net/unix/wwwboard/forum/5887.html

SampleCode:

/**
* @param command the string containing the command to execute
*/
public void executeCommand(String command)
{
Process p;
try{
p=Runtime.getRuntime().exec(command);
BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream()));

int i=p.waitFor();
if(i==0)
System.out.println("Command executed successfully");
else
System.out.println("Failed to execute command");
String s=null;

while((s=in.readLine())!=null)
System.out.println(s);
}

R.K
0
 
LVL 23

Accepted Solution

by:
Siva Prasanna Kumar earned 250 total points
ID: 20280344
0
 
LVL 17

Assisted Solution

by:contactkarthi
contactkarthi earned 250 total points
ID: 20280354
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 17

Expert Comment

by:contactkarthi
ID: 20280364
>>rama_krishna580

the question is executing the command on aremote machine... i think yours will execute it locally
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 20280384
0
 
LVL 9

Expert Comment

by:ysnky
ID: 20283738
for example, ls

import java.io.InputStream;
import java.io.IOException;

public class foo
{
   public foo()
   {
       Runtime rt = Runtime.getRuntime();
       Process p = null;
       try
       {
           p = rt.exec( "ls -l" );
       }
       catch ( IOException ioe )
       {
           System.out.println( "Error executing file" );
       }
       InputStream output = p.getInputStream();
       System.out.println( output );
   }

   public static void main( String args[] )
   {
       foo f = new foo();
       System.exit( 0 );
   }
}

for more;
http://www.computing.net/unix/wwwboard/forum/5887.html
http://www.unix.com/shell-programming-scripting/13932-execute-command-unix-java-possible-not.html
http://forum.java.sun.com/thread.jspa?threadID=471076&messageID=2175621
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question