jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes running shell script on Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "running shell script on Java" Watch "running shell script on Java" New topic

running shell script on Java

Tafta Zani

Joined: Jun 01, 2005
Posts: 11
Hi there,

I'm trying to make a program that can gather information about hardware spec in a computer with Linux platform. I have a shell script that can gather that sort of information but i have a problem running it in Java.

My script contains piping (|) and redirection (> symbols and when i tried it in java it didn't work.

If i run

Process cmd_su = Runtime.getRuntime().exec("cat /proc/cpuinfo | grep proc");

BufferedReader ls_in = new BufferedReader(new InputStreamReader(cmd_su.getInputStream()));
try {
while ((ls_str = ls_in.readLine()) != null) {
catch (IOException e){

it didn't show anything in the screen

How do i make piping and redirection work inside Java?
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Hi Tafta,

After you executed your code did it return any kind of error message, if so it could help others help you if you also mention your error in the next post. On the other hand there might be no "proc" in your file, which I am sure is not the case.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Piping and redirection isn't working with Java.

A detailed diskussion is found here: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

You may capture the outputstream of cmd_su, and use it as Inputstream for a second process (grep).

Maybe reading the file and search it by java would be more easy in this case.

Tafta Zani

Joined: Jun 01, 2005
Posts: 11
Thank you for the information.
Java IO is still too complicated for me so i'll go try build my own parser.
Could you tell me a java class suitable for making a text parser?
I agree. Here's the link: http://aspose.com/file-tools
subject: running shell script on Java
jQuery in Action, 3rd edition