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

    Default Run Unix commands from Java

    Greeings all

    Im trying to excute a command from Java and direct the output to the main output screen or to another file .... can you please help with this ? can I use filewriter for this ?

    Here is my code....


    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class RunCommand {
    
        private static boolean str;
    
        public static void main(String args[]) throws IOException {
    
            File dir = new File("c:\\Redalert");
            Process proc = Runtime.getRuntime().exec("cmd /c  start dir", null, dir);
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    
              //  FileWriter fw = new FileWriter("c:\\output.txt");
    
            while (str = stdInput.readLine() != null) {
                System.out.println(str);
    
            }
        }
    }
    Thanks

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

    Default

    I didn't know Unix had a 'cmd' command ...

    kind regards,

    Jos

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

    Default

    Yes it has.... it works fine and excute the cmd command ... but Im unable to redirect the steam to a file ...

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Try looking also at the error stream. Maybe the program is complaining about something.

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

    Default

    Thank you ... but does this solve my problem.. ? the code doesnt generate errors ... :confused:

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    I don't know if it will solve your problem. What do you think? Would it be useful to see any error messages that were generated by the program being called?

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

    Default

    no doubt.. but no errors are generated... didnt help in my case
    Last edited by yahyaaa; 06-17-2010 at 03:22 PM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    To test your code, use an invalid value in: exec("cmd /c start dir", null, dir);
    like: exec("cmdXXXX /c start dir", null, dir);
    to see what happens on the two output streams.

  9. #9
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by yahyaaa View Post
    Yes it has.... it works fine and excute the cmd command ... but Im unable to redirect the steam to a file ...

    there are quite a lot of unix derivate and i would be a little bit more precautious with such statements. just tried your code on aix and there is no cmd. i will try to correct the code, be patient...

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    More debugging: Add a println() to show the lines read before writing them to a file.

  11. #11
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    ok, i just started the following code on aix

    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class RunCommand {
    
        private static boolean str;
    
        public static void main(String args[]) throws IOException {
    
            try {
    			File file = new File("text.txt");
    			BufferedReader in = new BufferedReader(new FileReader(file));
    			String str;
    			
    			while ((str = in.readLine()) != null) {
    				System.out.println(str);
    			}
    			in.close();
    		} catch (IOException e) {
    			System.out.println("An exception occured " + e);
    			return;
    		}
    
        }
    }

    and the content of the file text.txt in the current directory was print on the console. by calling the java program like this java RunCommand > newtext.txt the output was redirected to newtext.txt

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

    Default

    the problem her that this is a shell output, Itried to do as what you did "> output.txt" but the file was empty after excution

  13. #13
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by yahyaaa View Post
    the problem her that this is a shell output, Itried to do as what you did "> output.txt" but the file was empty after excution

    when you start your java application with java RunCommand there must be output to the terminal, otherwise the redirection > will have no effect.

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

Similar Threads

  1. problems with executing unix commands
    By shanky in forum Advanced Java
    Replies: 1
    Last Post: 02-18-2010, 05:36 AM
  2. Help file for Java commands?
    By tyang in forum New To Java
    Replies: 2
    Last Post: 01-31-2010, 05:09 AM
  3. Low-level java commands
    By artemff in forum New To Java
    Replies: 8
    Last Post: 01-01-2010, 06:23 AM
  4. Java Robot commands not working
    By CoolLove in forum Java Applets
    Replies: 2
    Last Post: 01-13-2009, 02:58 PM
  5. using java to login to unix
    By dowell in forum Advanced Java
    Replies: 4
    Last Post: 05-23-2008, 07:26 AM

Posting Permissions

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