Solved

Execute unix command remotely ?

Posted on 2007-11-14
8
1,575 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unable to start eclipse ? 17 133
bitbucket vs gitbucket 3 57
listing all functions in JavaScript 19 179
servlet  URL Rewriting 1 27
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now