I m enable to run Linux command by Java code,

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

  1. Mohit Mehral

    Mohit Mehral Guest

    Hi programmer's,
    i have made javaCode, program run perfectly but created file not having
    any data.i need a code which run perfectly and also save output of
    command in textfile.Please provide exect matiral,plz dnt send the link.
    Let me describe in brife:
    I need to play command " df -h > text.txt"
    below program create a file but never add data in text.txt
    i havent modifyed it for linux(red hat 9).
    *************************
    // 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 22, 2006
    #1
    1. Advertising

  2. On 22 Mar 2006 01:03:30 -0800, Mohit Mehral wrote:
    > i have made javaCode, program run perfectly but created file not
    > having any data.


    In that case I'd say your program doesn't run perfectly.

    You closed the FileOutputStream, but really you should be closing the
    PrintWriter instead when you've finished writing to it.

    /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 22, 2006
    #2
    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. Patrick
    Replies:
    1
    Views:
    646
  2. yongsing
    Replies:
    6
    Views:
    1,212
    Knute Johnson
    Feb 14, 2004
  3. Mohit Mehral
    Replies:
    10
    Views:
    1,177
    Alex Hunsley
    Mar 25, 2006
  4. Mohit Mehral
    Replies:
    3
    Views:
    1,238
    Nigel Wade
    Mar 24, 2006
  5. Sonu
    Replies:
    2
    Views:
    368
    Alex Popescu
    Aug 5, 2007
Loading...

Share This Page