Solved

Execute shell script from java

Posted on 2004-04-19
6
3,398 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
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Oops, I forgot to copy the inner class file, let me try now
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Expert Comment

by:john-at-7fff
Comment Utility
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
Comment Utility
IT worked.

Thanks a lot!!
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
8-)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now