Results 1 to 10 of 10
Thread: Direct CMD output to a file
- 01-31-2011, 08:51 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 28
- Rep Power
- 0
Direct CMD output to a file
Dear Forum
I want to direct the output of command "dir" to a file using java..
direct the cmd command to cmdout.txt fileJava Code:import java.io.*; public class Exec { public static void main(String[] args) throws IOException { Process p = Runtime.getRuntime().exec("cmd /c start dir"); // OutputStream outstr = p.getOutputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedWriter fw = new BufferedWriter(new FileWriter("c:\\cmdout.txt")); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); fw.write(line); } fw.close(); } }
Can you please help...
- 01-31-2011, 09:44 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
dir command list the content, and seems to me you want to read something to a file. What's the connection?
- 01-31-2011, 09:48 AM #3
Member
- Join Date
- Sep 2008
- Posts
- 28
- Rep Power
- 0
Yes exactly, this is a simple program that I wrote, im not using a connection.. it just a normal cmd... I will then use tellnet command to excute command from client to SUN server
- 01-31-2011, 09:56 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
No, what I'm asking is what you want to do exactly?
- 01-31-2011, 10:21 AM #5
Member
- Join Date
- Sep 2008
- Posts
- 28
- Rep Power
- 0
ok.. Im sorry..
I want to telnet to a unix based server and excute command (ex. ls- ltr) and I want to set the output in buffer to use it in that program
- 01-31-2011, 10:29 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
That's something you want to execute a program in a different machine, is it?
If so, you cannot do that in a direct way. You may need to run your own client in that machine, and manipulate that with HTTP request or similar.
- 01-31-2011, 10:39 AM #7
Member
- Join Date
- Sep 2008
- Posts
- 28
- Rep Power
- 0
Thank you for your input,
But wht if Im doing it in same machine, as in my example, I'm excuting a basic cmd command but im not able to direct the output to a file..
Thanks again
- 01-31-2011, 11:02 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 01-31-2011, 11:10 AM #9
Member
- Join Date
- Sep 2008
- Posts
- 28
- Rep Power
- 0
You were absolutely right.. it was "start" command...
but why ? what was the reason behind it ?
Also, if I want to excute a command on a unix machine ? how can I open the shell ?
really appreciate your help
- 01-31-2011, 11:19 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
The 'start' command (as its name implies) starts another process; my guess was that it supplies new stdin, stdout and stderr streams for the new process. Your Java process listens to the stdin, stdout and stderr streams of the start command itself.
You can open a unix shell just like you can start a shell/command line interpreter on an MS Windows machine, e.g. bash, csh, bsh, whatever.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
how to get the resultset output into an output file
By renu in forum New To JavaReplies: 0Last Post: 09-30-2010, 08:16 PM -
how to change the layout of an input file and write to an output file
By renu in forum New To JavaReplies: 8Last Post: 05-12-2010, 07:19 PM -
Search a word(taken from one file) in another file and give the line as the output
By SwapnaNaidu in forum New To JavaReplies: 7Last Post: 11-19-2008, 02:09 PM -
how direct her/ him to the Sun´s download page?
By willemjav in forum Java AppletsReplies: 1Last Post: 04-17-2008, 04:03 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks