Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Command Line

  1. #1
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Command Line

    Hi,

    I have a method working fine that runs and executes command line arguments using the Process, Runtime and StreamPumpter classes with certain methods, the code is referred below. The problem is, when i try running the 'dir' command, it doesn't return any directories or files when I test it under main method? Although other commands work fine.
    I'm assuming it cant find where its trying to look?
    Thanks.

    static int execCmd(String cmdLine) throws Exception
    {
    Process process = Runtime.getRuntime().exec(cmdLine);

    StreamPumper outPumper = new StreamPumper(process.getInputStream(), System.out);

    StreamPumper errPumper = new StreamPumper(process.getErrorStream(), System.err);

    outPumper.start();

    errPumper.start();

    process.waitFor();

    outPumper.join();

    errPumper.join();

    return process.exitValue();
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Command Line

    Does the method throw any exception? What is it?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Command Line

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Command Line

    Yes, they throw IOException and InterruptedException exceptions.
    IOException is for the Runtime exec() method.
    InterruptedException is for the Process methods.

    Would that matter?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Command Line

    Yes, exceptions matter because they can be quite informative. The IOException comes with a message that more or less says what the problem is. It's a little cryptic, as such messages tend to be, but ask if you can't understand it.

    Java Code:
    import java.io.IOException;
    
    public class Foo {
        public static void main(String[] args) throws IOException {
            Runtime.getRuntime().exec("c:/Program Files/putty.exe");
            Runtime.getRuntime().exec("dir");
        }
    }
    "c:/Program Files/putty.exe" runs just fine, but "dir" throws an exception and says why.
    dougie1809 likes this.

  6. #6
    waiheng1986 is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: Command Line

    Quote Originally Posted by pbrockway2 View Post
    Yes, exceptions matter because they can be quite informative. The IOException comes with a message that more or less says what the problem is. It's a little cryptic, as such messages tend to be, but ask if you can't understand it.

    Java Code:
    import java.io.IOException;
    
    public class Foo {
        public static void main(String[] args) throws IOException {
            Runtime.getRuntime().exec("c:/Program Files/putty.exe");
            Runtime.getRuntime().exec("dir");
        }
    }
    "c:/Program Files/putty.exe" runs just fine, but "dir" throws an exception and says why.
    Code below is the example to execute the command in the command prompt.
    Java Code:
    import java.io.*; 
    
    public class doscmd 
    { 
    public static void main(String args[]) 
    { 
    try 
    { 
    Process p=Runtime.getRuntime().exec("cmd /c dir"); 
    p.waitFor(); 
    BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream())); 
    String line=reader.readLine(); 
    while(line!=null) 
    { 
    System.out.println(line); 
    line=reader.readLine(); 
    } 
    
    } 
    catch(IOException e1) {} 
    catch(InterruptedException e2) {} 
    
    System.out.println("Done"); 
    } 
    }
    Moderator edit: link removed.
    Last edited by waiheng1986; 03-12-2012 at 10:54 AM.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Command Line

    Quote Originally Posted by waiheng1986 View Post
    I am writing a blog about learning Java from basic.
    Kindly have a look if you are interested and raise up any question about Java in the blog and i will try to solve for it.
    Click here to the blog
    Moderator edit: link removed.
    waheng, this is a forum, not a free advertising site. If you want to promote your blog, we have a section for Reviews/Advertising.

    Any question about Java can as well be raised here as on your blog; rest assured that this forum has at least a few thousand times more readers.

    If you want to contribute to this site, you're most welcome. In view of this early posting history, any links to your blog are liable to be removed, and repeatedly posting links to the blog may lead to a ban.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    waiheng1986 is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: Command Line

    Quote Originally Posted by DarrylBurke View Post
    waheng, this is a forum, not a free advertising site. If you want to promote your blog, we have a section for Reviews/Advertising.

    Any question about Java can as well be raised here as on your blog; rest assured that this forum has at least a few thousand times more readers.

    If you want to contribute to this site, you're most welcome. In view of this early posting history, any links to your blog are liable to be removed, and repeatedly posting links to the blog may lead to a ban.

    db
    Well, sorry for the my foolish action in this forum. Actually i am honestly trying to help on those having problem in java and just promote the blog when i reply.
    I promise not to promote my blog in this forum and more and honest in solving others problem.

    Sorry and hope you guys can welcome me.
    Thank you

Similar Threads

  1. Command line argument
    By denisatandi in forum New To Java
    Replies: 8
    Last Post: 10-16-2012, 11:37 PM
  2. Replies: 16
    Last Post: 09-17-2011, 02:41 AM
  3. can i run line by line command in netbean?
    By choconlongxu in forum NetBeans
    Replies: 1
    Last Post: 07-19-2010, 08:41 PM
  4. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:23 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
  •