Skip to content

Execute command prompt thru Java

October 19, 2007


public class TryTry
public static void main(String[] args) throws IOException
String fileName = “C:\\Documents and Settings\\User\\My Documents\\car.txt”;

String to = “C:\\Documents and Settings\\User\\My Documents\\My Videos”;

String[] commands = {“cmd”, “/c”, “copy”, fileName ,to };

String cmdline = “copy ” + fileName + ” ” + to;



catch(Exception e)




From → Java

  1. Thanks for the little example.

    I am doing Eclipse RCP development and I need to have a Command Line interface besides the GUI.

    Being able to open files with windows associated editor through the CLI with a one liner (“cmd /c start fileName”) is really nice.

    Thanks again

  2. Is there any way to get the result of the command executed to be stored to a variable .
    Ex: we will get a message the file is in use by another program if we try to run an erase comman on a open file.
    I want to get that message to a variable could you please put some light on this….

  3. Sorry, i don’t really get what you mean.. Do u mind to elaborate?

  4. thanks for this tutorial

  5. sucharitha permalink

    i have problem in running java files in command prompt.

    i am able to compile ,but when i am running the file i am getting this error

    Error could not open: C:\Program Files\Java\jdk1.6.0_22\jre\lib\i386\jvm.cfg file

    could you please help.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: