CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Runtime.exec()

  1. #1
    Guest

    Runtime.exec()

    Hi all,

    Could someone please help me with this: I am trying to run a dos/unix command within my java application by using:

    String command = "ping hostname";
    try {
    (Runtime.getRuntime()).exec(command);
    }
    catch (Exception e) {}

    It works fine except for the fact that I don't know how to get the output. For instance, when I issue the command "Ping hostname", it would execute successufully, but I'd like to have the output redirected to a text file: ie:
    (Runtime.getRuntime()).exec("ping > result.txt");
    I thought that this method should work, but it seems that no output text file is being generated.

    Any help on this would be greatly appreciated.

    TN




  2. #2
    Join Date
    May 1999
    Location
    Pune, MH, India.
    Posts
    453

    Re: Runtime.exec()

    Well Runtime.exec() method returns 'Process' instance. And 'Process' class has getInputStream, getOutputStream etc. methods. Have u tried catching output using 'getInputStream' and then by reading from the stream?

    - UnicMan
    http://members.tripod.com/unicman

  3. #3
    Guest

    Re: Runtime.exec()

    Hi unicman,
    Thanks for your input, it really works. But now I just wonder if there is a way to capture this output to a string instead of a file (just to avoid applet security manager). Any thought on this will be appreciated.

    Thanks in advance
    TN


  4. #4
    Join Date
    May 1999
    Location
    Pune, MH, India.
    Posts
    453

    Re: Runtime.exec()

    Sorry for the delay in response.

    Yes it is possible to capture the output in a string. U get 'InputStream' and attach it to 'DataInputStream' and then u can read line by line if u want or go on appending the string and after all the data has come, start processing it.

    I didn't understand why u stored the data in a file.

    - UnicMan
    http://members.tripod.com/unicman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured