Solved

Execute shell script from java

Posted on 2004-04-19
6
3,410 Views
Last Modified: 2013-11-24
Hi I need to exectue a one line unix (solaris) shell script from my java servlet. I tried using the following code, but got a "broken pipe" IOException. Any idea? Thanks in advance

 try
    {
      Runtime rt = Runtime.getRuntime();
      Process child = rt.exec("rsh -l pgg mymachine \"/usr/local/test_ServerClient 2 10001 \"");
   
      BufferedWriter outCommand = new BufferedWriter(new OutputStreamWriter(child.getOutputStream()));
      outCommand.write("MyShellScript");
      child.waitFor();
      int retCode = child.exitValue();
      outCommand.flush();
     }
    catch(InterruptedException ine)
    {
      System.out.println(ine.getMessage());
    }
    catch(IOException ie)
    {
      System.out.println(ie.getMessage());
    }
0
Comment
Question by:javagirip
[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
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 10858726
Try running it with this class:



public class CommandRunner {

      private int returnValue = Integer.MIN_VALUE;



      public int getReturnValue() {
            return returnValue;
      }



      public static void main(String[] args) {
            new CommandRunner().run(args);
      }






      public void run(String[] args) {

            try {

                  if (args.length < 1) {
                        System.out.println("Usage: java CommandRunner <command string>");
                        System.exit(-1);
                  }
                  Process pro = null;
                  if (args.length > 1) {
                        pro = Runtime.getRuntime().exec(args);
                  }
                  else {
                        pro = Runtime.getRuntime().exec(args[0]);
                  }
                  InputStream error = pro.getErrorStream();
                  InputStream output = pro.getInputStream();
                  Thread err = new Thread(new OutErrReader(error));
                  Thread out = new Thread(new OutErrReader(output));
                  out.start();
                  err.start();
                  returnValue = pro.waitFor();
            }
            catch (java.io.IOException e) {
                  e.printStackTrace();
            }
            catch (java.lang.InterruptedException e) {
                  e.printStackTrace();
            }

      }


      class OutErrReader implements Runnable {
            InputStream is;


            public OutErrReader(InputStream is) {
                  this.is = is;
            }


            public void run() {
                  try {
                        BufferedReader in = new BufferedReader(new InputStreamReader(is));
                        String temp = null;
                        while ((temp = in.readLine()) != null) {
                              System.out.println(temp);
                        }
                        is.close();
                  }
                  catch (Exception e) {
                        e.printStackTrace();
                  }
            }
      }
}
0
 

Author Comment

by:javagirip
ID: 10858907
I tried your code, but getting the following exception

java.lang.NoClassDefFoundError: adb/CommandRunner$OutErrReader

at the following line of the code

Thread err = new Thread(new OutErrReader(error));

0
 

Author Comment

by:javagirip
ID: 10858921
Oops, I forgot to copy the inner class file, let me try now
0
Industry Leaders: 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 4

Expert Comment

by:john-at-7fff
ID: 10859067
If CEHJ's suggestions doesn't work, note that Runtime.exec() runs differently on different systems. On Solaris and AIX, you usually have to use the alternative method form, where you pass in an array:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#exec(java.lang.String[],%20java.lang.String[],%20java.io.File)

(If that URL doesn't work in your browser, go to http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html and skip down to the version of exec with the signature: exec(String[] cmdarray, String[] envp)

Here's a discussion:

http://forum.java.sun.com/thread.jsp?thread=57009&forum=31&message=144444

0
 

Author Comment

by:javagirip
ID: 10859895
IT worked.

Thanks a lot!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10860236
8-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
Java Restore security prompts not working 10 233
Is Applet the way to go for my drag and drop system? 8 48
intellij error 2 51
jmeter usage 4 55
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

740 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