tags:

views:

2020

answers:

5

Hi,

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?

+1  A: 

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
I don't understand... How do you know what is in his shell script? He hasn't posted it
oxbow_lakes
String [] cmd = {"cmd.exe", "/C","D:/Development/saveReport.bat"}it open the command prompt and execute teh batach fileThe same thing i need to do while running the batch file
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
i have tried but it is not working
@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
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
http://en.wikipedia.org/wiki/Shebang_(Unix)
SourceRebels
+1  A: 

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
i should see the terminal and then the output
Do you mean the Spielberg film: "The Terminal"? Or are we talking about another terminal here?
oxbow_lakes
@oxbow_lakes: In Windows it's called a command prompt window, in Linux it's called a shell or terminal. Same thing.
Michael Myers
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
Too get the output from the process, you need getInputStream, not getOutputStream...
Ben Lings
+3  A: 

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
+1  A: 
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
A: 

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.

extraneon