2

I have a shell script named test.sh in /tmp/padm folder. In that shell script I have a single statement

echo "good"

I am trying to run the shell script using Java code.

String cmd=("/tmp/padm/.test.sh");            
Runtime rt = Runtime.getRuntime();
Process pr=rt.exec(cmd);

But my problem is that I am not able to see "good" which is the output of the shell script.

How can I get the script to run?

Michael Myers
  • 188,989
  • 46
  • 291
  • 292

6 Answers6

4

You could get the command output with the following piece of code. Hope this helps.

Process process = Runtime.getRuntime().exec(cmd);
String s = "";
BufferedReader br = new BufferedReader(new InputStreamReader(process
            .getInputStream()));
while ((s = br.readLine()) != null)
{
   s += s + "\n";
}    
System.out.println(s);

BufferedReader br2 = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while (br2.ready() && (s = br2.readLine()) != null)
{
  errOutput += s;
}
System.out.println(errOutput);
Snehal
  • 7,266
  • 2
  • 32
  • 42
1

Try this, it will definitely works.

Shell Script test.sh code

   #!/bin/sh
   echo "good"

Java Code to execute shell script test.sh

          try {
                Runtime rt = Runtime.getRuntime();
                Process pr = rt.exec(new String[]{"/bin/sh", "/tmp/padm/.test.sh"});

                BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
                String line = "";
                while ((line = input.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (Exception e) {
                System.out.println(e.toString());
                e.printStackTrace();
            }
Suniel
  • 1,449
  • 8
  • 27
  • 39
1

That won't work. You must add either a "hash bang" to the first line of the script, to tell Linux that it must use a suitable interpreter (e.g. bash) to interpret the script, or run it through bash explicitly from Java.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • I don't understand... How do you know what is in his shell script? He hasn't posted it – oxbow_lakes Mar 04 '09 at 09:48
  • String [] cmd = {"cmd.exe", "/C","D:/Development/saveReport.bat"} it open the command prompt and execute teh batach file The same thing i need to do while running the batch file –  Mar 04 '09 at 10:05
  • I'm not sure that there is an analog of this in the linux environment! Do what I suggested and grab the process output using process.getOutputStream(). – oxbow_lakes Mar 04 '09 at 10:19
  • @oxbow_lakes: oh? I interpreted 'in the shell script i have the following echo "good"' as a listing of the script, i.e. it's just a single echo. – unwind Mar 04 '09 at 10:51
  • If the script is executable, it should work. It may work quicker (one less failed system call) with #!, but it should work without too. – Jonathan Leffler Mar 05 '09 at 15:24
1

When you say "on the terminal" what do you mean? If you want to see output/error from the process you'll need to use:

process.getErrorStream();
process.getOutputStream();

Other than that, there's no problem I can see from your use of Runtime.exec to invoke a shell script

oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
  • i should see the terminal and then the output –  Mar 04 '09 at 10:01
  • Do you mean the Spielberg film: "The Terminal"? Or are we talking about another terminal here? – oxbow_lakes Mar 04 '09 at 10:17
  • @oxbow_lakes: In Windows it's called a command prompt window, in Linux it's called a shell or terminal. Same thing. – Michael Myers Mar 04 '09 at 14:31
  • I don't think he means that though; it sounds like when he runs something on linux, he's expecting some window to magically pop up somewhere. Like it does on a Windows box when you use java and not javaw. – oxbow_lakes Mar 04 '09 at 15:51
  • Too get the output from the process, you need getInputStream, not getOutputStream... – Ben Lings Sep 19 '10 at 13:11
1
process.getErrorStream();
process.getOutputStream();

is the right approach as pointed out by oxbow_lakes.

Additionally make sure that you exec /bin/sh with the shell script location as an argument.

Shyam Kumar Sundarakumar
  • 5,649
  • 13
  • 42
  • 69
0

This is a duplicate of Need sample java code to run a shellscript.

The example program in my answer there does print out standard error and standard output (the example program is added a bit later).

Please note that the streamGobblers are running in separate threads to prevent execution problems due to full input/output buffers.

If you wish you can of course let the StreamGobblers store the output in lists, and retrieve the lists after the process executed in stead of dumping it directly on stdout.

Community
  • 1
  • 1
extraneon
  • 23,575
  • 2
  • 47
  • 51