Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to call shellscript from java program

Posted on 2004-04-28
10
Medium Priority
?
695 Views
Last Modified: 2012-06-21
how to call shell script from java program and i need to pass some parameters also to that script.it's urgent
0
Comment
Question by:vsita
[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
10 Comments
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10937440
use

Runtime runtime = Runtime.getRuntime();

String[] cmd = {"aaa.sh", "param1", "param2", .....};
Process p = runtime.exec(cmd);

This way, you can execute the shell script from Java.

If you want to see the output of the process

then
InputStream in = process.getInputStream();
in.read();

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10937566
Pass a String array of arguments to this:


package net.proteanit.process;

import java.io.*;

/**
 *  Runs a program
 *
 * @author     CEHJ
 * @created    23 February 2004
 */
public class CommandRunner {

      private int returnValue = Integer.MIN_VALUE;



      /**
       *  Gets the returnValue attribute of the CommandRunner object
       *
       * @return    The returnValue value
       */
      public int getReturnValue() {
            return returnValue;
      }



      /**
       *  The main program for the CommandRunner class
       *  For testing only
       *
       * @param  args  The command line arguments
       */
      public static void main(String[] args) {
            new CommandRunner().run(args);
      }





      /**
       *  Description of the Method
       *
       * @param  args  Description of the Parameter
       */

      /**
       *  Main processing method for the CommandRunner object
       *
       * @param  args  Description of the Parameter
       */
      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();
            }

      }


      /**
       *  Description of the Class
       *
       * @author     Administrator
       * @created    23 February 2004
       */
      class OutErrReader implements Runnable {
            InputStream is;


            /**
             *Constructor for the OutErrReader object
             *
             * @param  is  Description of the Parameter
             */
            public OutErrReader(InputStream is) {
                  this.is = is;
            }


            /**
             *  Main processing method for the OutErrReader object
             */
            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:vsita
ID: 10937634
my java program is

Runtime runtime = Runtime.getRuntime();
String[] cmd = {"run.sh", "5"};
Process p = runtime.exec(cmd);


insdie run.sh i am accesing that value "5" and want to echo that value at the command prompt and also write to some file.

my run.sh is

code=$1
echo  code
code > inputs.txt

whether this code is correct or not.
i am not able to get the value at the command prompt and also value is not written to the file
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10937645
you won't get anything at command prompt...


Runtime runtime = Runtime.getRuntime();
String[] cmd = {"run.sh", "5"};
Process p = runtime.exec(cmd);

InputStream in = p.getInputStream();

int ch ;

while ((ch = in.read()) != -1)
{
       System.out.println((char)ch);
}


Add this code, you can see the echo part in the command prompt ....
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10937646
The last line should read:

echo code > inputs.txt

;JOOP!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10937647
Using the program above:

CommandRunner.run(new String[] { "sh", "run.sh", "5" });
0
 
LVL 9

Accepted Solution

by:
mmuruganandam earned 500 total points
ID: 10937653
InputStream in = p.getInputStream();

int ch ;

while ((ch = in.read()) != -1)
{
       System.out.println((char)ch);
}

This is the part of code, which is used to get the output of the launched process....

So if you read the input stream of the process and prints out the contents, then you can able to see the echo part in your command prompt.


Regards,
Muruga
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10937659
(assuming the name of the executable of your shell interpreter is 'sh' )
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10937663
>> echo code > inputs.txt

This will move the "5" to inputs.txt


You won't find anything in the command prompt then...

change that to

echo code
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10937786
thanx :-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

721 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