Execute shell script from java

Posted on 2004-04-19
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

      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()));
      int retCode = child.exitValue();
    catch(InterruptedException ine)
    catch(IOException ie)
Question by:javagirip
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
LVL 86

Accepted Solution

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>");
                  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));
                  returnValue = pro.waitFor();
            catch ( e) {
            catch (java.lang.InterruptedException e) {


      class OutErrReader implements Runnable {
            InputStream is;

            public OutErrReader(InputStream is) {
         = is;

            public void run() {
                  try {
                        BufferedReader in = new BufferedReader(new InputStreamReader(is));
                        String temp = null;
                        while ((temp = in.readLine()) != null) {
                  catch (Exception e) {

Author Comment

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));


Author Comment

ID: 10858921
Oops, I forgot to copy the inner class file, let me try now
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!


Expert Comment

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:[],%20java.lang.String[],

(If that URL doesn't work in your browser, go to and skip down to the version of exec with the signature: exec(String[] cmdarray, String[] envp)

Here's a discussion:


Author Comment

ID: 10859895
IT worked.

Thanks a lot!!
LVL 86

Expert Comment

ID: 10860236

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