Results 1 to 10 of 10
  1. #1
    yahyaaa is offline Member
    Join Date
    Sep 2008
    Posts
    28
    Rep Power
    0

    Default Direct CMD output to a file

    Dear Forum

    I want to direct the output of command "dir" to a file using java..


    Java 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();
    
        }
    }
    direct the cmd command to cmdout.txt file

    Can you please help...

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    dir command list the content, and seems to me you want to read something to a file. What's the connection?

  3. #3
    yahyaaa is offline Member
    Join Date
    Sep 2008
    Posts
    28
    Rep Power
    0

    Default

    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

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    yahyaaa is offline Member
    Join Date
    Sep 2008
    Posts
    28
    Rep Power
    0

    Default

    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

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  7. #7
    yahyaaa is offline Member
    Join Date
    Sep 2008
    Posts
    28
    Rep Power
    0

    Default

    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

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by yahyaaa View Post
    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..
    My guess is that the 'start' command spoils the show; try "cmd /c dir" instead.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    yahyaaa is offline Member
    Join Date
    Sep 2008
    Posts
    28
    Rep Power
    0

    Default

    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

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by yahyaaa View Post
    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
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 09-30-2010, 08:16 PM
  2. Replies: 8
    Last Post: 05-12-2010, 07:19 PM
  3. Replies: 7
    Last Post: 11-19-2008, 02:09 PM
  4. how direct her/ him to the Sun´s download page?
    By willemjav in forum Java Applets
    Replies: 1
    Last Post: 04-17-2008, 04:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •