The Artima Developer Community
Sponsored Link

Java Answers Forum
execute Unix commands

2 replies on 1 page. Most recent reply: May 7, 2004 1:09 AM by gabri

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
gabri

Posts: 9
Nickname: gabri
Registered: Jan, 2004

execute Unix commands Posted: Apr 6, 2004 9:43 AM
Reply to this message Reply
Advertisement
Hello!
I have a distributed application in Java. Im implementing a remote Unix shell using RMI. The server-client communication is working fine it seems. The part which i donno is how to execute the Unix commands received frm the client. I thought of using RunTime - exec but i guess it wont give me enough control over the output of the shell. My goal is to cover all three posibilities:
1.correct command... the output is returned to the client
2.incorrect command.. the error message frm the shell..its output is returned
3.inexistent command.. unable to execute.. a corresponding message is sent
Any ideas on how i should approach this?
Thank u,
Gabri


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: execute Unix commands Posted: Apr 11, 2004 12:07 AM
Reply to this message Reply
      runtime = Runtime.getRuntime();
      process = runtime.exec(cmdInstruction);
 
      in = new BufferedReader(new InputStreamReader(process.getInputStream()));
      err= new BufferedReader(new InputStreamReader(process.getErrorStream()));
 
      String buffer;
      while((buffer = err.readLine())!= null) {
        returnVal.append(buffer);
        returnVal.append("\n");
      }
 
      if (returnVal.length() != 0){
        return returnVal.insert(0,"Error:");
      }
 
      // the Success message from the OS is read into a buffer and returned
      while((buffer = in.readLine())!= null) {
        returnVal.append(buffer);
        returnVal.append("\n");
      }
 


> 1.correct command... the output is returned to the client
if there is nothing in the error stream you can read the input stream for the result returned by the shell.

> 2.incorrect command.. the error message frm the shell..its
> output is returned

read the error stream and if there is data in the error stream then that means shell returned an error

> 3.inexistent command.. unable to execute.. a corresponding
> message is sent

This will also have data in the error stream I guess. but I am not sure. you have to test it and see.

gabri

Posts: 9
Nickname: gabri
Registered: Jan, 2004

Re: execute Unix commands Posted: May 7, 2004 1:09 AM
Reply to this message Reply
Sorry for very late reply. Thanking you very much. It worked out very well.
Regards,
Gabri

Flat View: This topic has 2 replies on 1 page
Topic: Problem with input from keyboard Previous Topic   Next Topic Topic: Setting java.library.path during runtime

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use