?
Solved

Execute unix command remotely ?

Posted on 2007-11-14
8
Medium Priority
?
1,587 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 total points
ID: 20280344
0
 
LVL 17

Assisted Solution

by:contactkarthi
contactkarthi earned 1000 total points
ID: 20280354
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

649 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