Solved

Execute unix command remotely ?

Posted on 2007-11-14
8
1,581 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
Tomcat: Unable to run tomcat service. 2 23
ejb on wildfly 5 30
String array comparison 4 38
restrict decimal places for double datatype 10 22
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

829 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