Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java and dos commands

Options
  • 05-12-2002 1:27pm
    #1
    Registered Users Posts: 919 ✭✭✭


    This like everything is for college and I simply need help getting one part to work.

    I need to make a java command shell that passes commands to a dos command prompt and displays the results. Theres a bit more then this to it but this is the part i'm having problems with and once its sorted the rest is easy.

    Did a google for it and came up with this section of code:

    import java.io.*;

    public class Test
    {
    public static void main( String[] args ) throws Exception
    {
    Process proc = Runtime.getRuntime().exec( "cmd /c "cd
    \projects && dir"" );

    InputStream in = proc.getInputStream();

    int charIn;

    while( (charIn = in.read()) != -1 )
    {
    System.out.print( (char)charIn );
    }
    }
    }


    This kinda works but not really. Try the code and you'll see what I mean.

    Any and all help greatly welcomed
    Timeout


Comments

  • Closed Accounts Posts: 358 ✭✭CH


    Runtime.exec() is not a command line
    One final pitfall to cover with Runtime.exec() is mistakenly assuming that exec() accepts any String that your command line (or shell) accepts. Runtime.exec() is much more limited and not cross-platform. This pitfall is caused by users attempting to use the exec() method to accept a single String as a command line would. The confusion may be due to the fact that command is the parameter name for the exec() method. Thus, the programmer incorrectly associates the parameter command with anything that he or she can type on a command line, instead of associating it with a single program and its arguments.
    ...taken from this article

    Maybe if you were to get around this problem by writing a batch file and call that using Runtime.exec().

    Regards.


  • Registered Users Posts: 919 ✭✭✭timeout


    I was actually on that site and did not find this article.

    It has helped. Has gotton me closer to figuring this thing out.

    Thank you
    Timeout


  • Registered Users Posts: 2,010 ✭✭✭Dr_Teeth


    Ant (jakarta.apache.org) is a build tool that features an <exec> tag you can use in your build files to run arbitrary system commands. I use it for running an app server's deploy tool after compiling. Maybe you can have a look at the Ant source and see how they do it?

    Teeth.


  • Registered Users Posts: 2,010 ✭✭✭Dr_Teeth


    I saw this, and thought of you :)

    http://wireless.java.sun.com/midp/ttips/proguard/

    Have a look at the 'run' and 'redirect' methods of the code sample.

    Teeth.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    might not be related.
    "cmd /c "cd \projects && dir""
    

    shouldn't that be
    "cmd /c [b]"[/b]"cd \projects && dir""
    


  • Advertisement
  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    I think you need another " at the end there.

    jc


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    that too.

    Now that I think of it again, prehaps this?
    "cmd /c "[b]"[/b]cd \[b]\[/b]projects && dir""[b]"[/b]
    


  • Registered Users Posts: 919 ✭✭✭timeout


    Thanks for all the replys.

    I managed to get it running a couple of commands. All i need to do is tidy it up and add the other features.

    might post the finished code.

    Timeout


Advertisement