Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Running system commands from within Java

  Asked By: Juana    Date: Aug 02    Category: Java    Views: 1069

I need to run the Top Unix utility from within Java. I have tried to
run top using the following code, but keep on getting the run time
exception, "Exception in thread "main" java.lang.NoSuchMethodError:

I'm using the following *code*:

public class TopRunner
public static void main (String args)

Runtime.getRuntime().exec("/usr/bin/top >>
catch (IOException ioe)



4 Answers Found

Answer #1    Answered By: Jet Brown     Answered On: Aug 02

you haven't defined the main() method correctly. You have

public static  void main  (String args)

it should be

public static void  main(String [] args)

I haven't tested your program, but looks ok otherwise

Answer #2    Answered By: Ludkhannah Fischer     Answered On: Aug 02

yeah, I just realised that after I had sent the msg.

Now the problem is I need the Top process to end so that the data
collected can be stored to the required output file. Hence, does
anyone know if it's possible to issues commands  like Control-C using
this approach. If so, how??

Answer #3    Answered By: Sairish Kauser     Answered On: Aug 02

you can get a reference to the process like this

Process p = Runtime.getRuntime().exec("/usr/bin/top");

then read its output something like this

BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

you could try using the getOutputStream() method to get an OutputStream you
could write your 'q' or control-c command to the 'top' process.

Answer #4    Answered By: Javairea Akram     Answered On: Aug 02

After much frustration I decided to call Process.getErrorSteam(),
which gives outputs:
stty: standard input: Invalid argument
top: tcgetattr() failed: Invalid argument

Does anyone know what the exact problem is here?? I've looked at the
man and info pages for stty, but haven't been able to pin point the

Didn't find what you were looking for? Find more on Running system commands from within Java Or get search suggestion and latest updates.