How the Output save in text.txt file,I m able to run Linux command by java code

Discussion in 'Java' started by Mohit Mehral, Mar 23, 2006.

  1. Mohit Mehral

    Mohit Mehral Guest

    Hi Professionals,
    Please guide me how to run linux command(df -h > text.txt) with
    director by JavaCode.
    i have made code ....which run the file but we unable to write all
    information to text file.
    code below.

    // GoodWinRedirect.java
    import java.util.*;
    import java.io.*;

    class StreamGobbler extends Thread
    {
    InputStream is;
    String type;
    OutputStream os;

    StreamGobbler(InputStream is, String type)
    {
    this(is, type, null);
    }

    StreamGobbler(InputStream is, String type, OutputStream redirect)
    {
    this.is = is;
    this.type = type;
    this.os = redirect;
    }

    public void run()
    {
    try
    {
    PrintWriter pw = null;
    if (os != null)
    pw = new PrintWriter(os);

    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line=null;
    while ( (line = br.readLine()) != null)
    {
    if (pw != null)
    pw.println(line);
    System.out.println(type + ">" + line);
    }
    if (pw != null)
    pw.flush();
    } catch (IOException ioe)
    {
    ioe.printStackTrace();
    }
    }
    }

    public class GoodWinRedirect
    {
    public static void main(String args[])
    {
    if (args.length < 1)
    {
    System.out.println("USAGE java GoodWinRedirect
    <outputfile>");
    System.exit(1);
    }

    try
    {
    FileOutputStream fos = new FileOutputStream(args[0]);
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec("java jecho 'Hello World'");
    // any error message?
    StreamGobbler errorGobbler = new
    StreamGobbler(proc.getErrorStream(), "ERROR");


    // any output?
    StreamGobbler outputGobbler = new
    StreamGobbler(proc.getInputStream(), "OUTPUT", fos);

    // kick them off
    errorGobbler.start();
    outputGobbler.start();

    // any error???
    int exitVal = proc.waitFor();
    System.out.println("ExitValue: " + exitVal);
    fos.flush();
    fos.close();
    } catch (Throwable t)
    {
    t.printStackTrace();
    }
    }
    }
     
    Mohit Mehral, Mar 23, 2006
    #1
    1. Advertising

  2. On 23 Mar 2006 10:38:20 -0800, Mohit Mehral wrote:
    > Please guide me how to run linux command(df -h > text.txt) with
    > director by JavaCode.


    Please - read the answers you have already got before posting the same
    question over and over...

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Mar 23, 2006
    #2
    1. Advertising

  3. Mohit Mehral

    Mohit Mehral Guest

    i have tried ur instruction but the result same....
    please modify this wxample for linux if u can...
     
    Mohit Mehral, Mar 23, 2006
    #3
  4. Mohit Mehral

    Nigel Wade Guest

    Mohit Mehral wrote:

    > Hi Professionals,
    > Please guide me how to run linux command(df -h > text.txt) with
    > director by JavaCode.


    If you run the Linux command 'df -h > text.txt', there will be no output for
    your Java code to redirect.

    --
    Nigel Wade, System Administrator, Space Plasma Physics Group,
    University of Leicester, Leicester, LE1 7RH, UK
    E-mail :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
     
    Nigel Wade, Mar 24, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mohit Mehral
    Replies:
    1
    Views:
    5,995
    Gordon Beaton
    Mar 22, 2006
  2. Mohit Mehral
    Replies:
    10
    Views:
    1,217
    Alex Hunsley
    Mar 25, 2006
  3. Sameen
    Replies:
    2
    Views:
    466
    Victor Bazarov
    Aug 29, 2005
  4. Sonu
    Replies:
    2
    Views:
    396
    Alex Popescu
    Aug 5, 2007
  5. hycn office
    Replies:
    2
    Views:
    196
    WIlliam Morris
    Oct 10, 2003
Loading...

Share This Page