wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "running shell script on Java" Watch "running shell script on Java" New topic
Author

running shell script on Java

Tafta Zani
Greenhorn

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) {
System.out.println(ls_str);
}
}
catch (IOException e){
System.exit(0);
}

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.


http://home.arcor.de/hirnstrom/bewerbung
Tafta Zani
Greenhorn

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?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: running shell script on Java
 
Similar Threads
Runtime getRuntime() exec(cmd[]) - How does this work?
Runtime.getRuntime().exec(commandToExecute) without promting for enter key to be pressed
Running commands using Runtime.getRuntime().exec() in Unix
getRuntime().exec()
Running Unix commands thru Java