Execute command prompt thru Java
import java.io.*;
import java.io.File.*;
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 };
Runtime.getRuntime().exec(commands);
String cmdline = “copy ” + fileName + ” ” + to;
try{
Runtime.getRuntime().exec(commands);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
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
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….
Sorry, i don’t really get what you mean.. Do u mind to elaborate?
thanks for this tutorial
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.