Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Can i Run linux command with redirection by JavaCode(df -h > text.txt)

Thread view: 
Mohit Mehral - 23 Mar 2006 19:35 GMT
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();
         }
   }
}
Gordon Beaton - 23 Mar 2006 19:54 GMT
> 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.

I already answered this question for you the other day. Did you not
understand what I suggested? Here it is again...

You open a PrintWriter, but you never close it. You *must* close the
PrintWriter! It is not sufficient to close the FileOutputStream. Not
only that, you must close the PrintWriter *before* closing the
FileOutputStream.

/gordon

Signature

[  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

Mohit Mehral - 23 Mar 2006 20:00 GMT
Not corrct method ,which one sent by u , it was not work...
please modify the code if u can
Gordon Beaton - 23 Mar 2006 20:27 GMT
> Not corrct method ,which one sent by u , it was not work... please
> modify the code if u can

Don't run "outputGobbler" in a separate thread. Or make sure you wait
for it to finish (use join() from the main thread) before closing
*first* the PrintWriter and *then* the FileOutputStream.

/gordon

Signature

[  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

tom fredriksen - 24 Mar 2006 11:00 GMT
> Not corrct method ,which one sent by u , it was not work...
> please modify the code if u can

If what he suggests did not work for you, you should post a reply in the
same thread as you posted the original question. That way people can
tell that it did not work and can suggest new ways you can try to fix
it. Starting a new thread asking the same question, does not help you
fix the problem any faster.

/tom
Mohit Mehral - 24 Mar 2006 18:22 GMT
tom if u not want to help me....then why u should motivate to other,
they do job like u....
stop this nonsence....if u havnt exect information then please dont
arrgument with me....'
i dont want that ppl's whom not like to help other......u are
Realllyyyyy.....
Oliver Wong - 24 Mar 2006 18:36 GMT
> tom if u not want to help me....then why u should motivate to other,
> they do job like u....
> stop this nonsence....if u havnt exect information then please dont
> arrgument with me....'
> i dont want that ppl's whom not like to help other......u are
> Realllyyyyy.....

   Tom, Gordon, others, and myself ARE trying to help you. We're trying to
help you phrase your question so that someone might be able to answer it.

   If no one can understand what your problem is, no one will be able to
fix it.

   If you just keep posting the same question over and over again, we will
assume that you aren't actually reading the answers we're providing to you,
and start to ignore you, and then you'll REALLY be in big trouble, 'cause
you're won't be getting any answers, and you won't know why.

   - Oliver
Mohit Mehral - 24 Mar 2006 20:44 GMT
Thanx Oliver,
i respect ur way to asked me the exect problem...and i m sure it will
not  happ. again
there are lot of diffrent between ur's and Tom words.....that way i
angry with Tom.
Anyway Thanx a lot guys,i have solved the problem at least...
if u all want then i will paste the code .....code is ready for any
o.s.
HaVe A Nice TiMe.
Roedy Green - 24 Mar 2006 19:13 GMT
>tom if u not want to help me....then why u should motivate to other,
>they do job like u....
>stop this nonsence....if u havnt exect information then please dont
>arrgument with me....'
>i dont want that ppl's whom not like to help other......u are
>Realllyyyyy.....

Please see http://mindprod.com/jgloss/newsgroups.html
You are doing several things that are reducing your odds of getting
help.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Mohit Mehral - 24 Mar 2006 20:50 GMT
Thanx  Roedy..
I m really apoloyies for that,i got angry becoz of Tom words,that why
it happ....And i m sure it will not happ again
anyway thax for above link
Alex Hunsley - 25 Mar 2006 02:33 GMT
> tom if u not want to help me....then why u should motivate to other,
> they do job like u....
> stop this nonsence....if u havnt exect information then please dont
> arrgument with me....'

They are not arguing with you, they are trying to help you.

> i dont want that ppl's whom not like to help other......u are
> Realllyyyyy.....

Please try writing complete sentences and using english words like "you"
(and not 'u'). It helps people to understand you, and hence help you.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.