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

M

Mohit Mehral

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();
}
}
}
 
G

Gordon Beaton

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
 
M

Mohit Mehral

Not corrct method ,which one sent by u , it was not work...
please modify the code if u can
 
G

Gordon Beaton

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
 
T

tom fredriksen

Mohit said:
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
 
M

Mohit Mehral

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.....
 
O

Oliver Wong

Mohit Mehral said:
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
 
R

Roedy Green

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.
 
M

Mohit Mehral

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.
 
M

Mohit Mehral

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
 
A

Alex Hunsley

Mohit said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top