Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How the Output save in text.txt file,I m able to run Linux command by java code

Reply
Thread Tools

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

 
 
Mohit Mehral
Guest
Posts: n/a
 
      03-23-2006
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();
}
}
}

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      03-23-2006
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
 
Reply With Quote
 
 
 
 
Mohit Mehral
Guest
Posts: n/a
 
      03-23-2006
i have tried ur instruction but the result same....
please modify this wxample for linux if u can...

 
Reply With Quote
 
Nigel Wade
Guest
Posts: n/a
 
      03-24-2006
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 : http://www.velocityreviews.com/forums/(E-Mail Removed)
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Not able to display color in excel not able to display color in excel using xml sed_y XML 0 02-15-2012 09:46 PM
Linux net command showing incorrect output if executing from java. mickeyteg Java 0 11-04-2010 09:39 AM
how to run os.execv() to run command pslq dbname < gen.command Sonu Python 2 08-04-2007 11:25 PM
ExecuteWinApp not able to run the command kk.simhadri@gmail.com ASP General 0 10-11-2006 03:35 PM
I m enable to run Linux command by Java code, Mohit Mehral Java 1 03-22-2006 09:13 AM



Advertisments