Execute CMD commands through java program



java allows us to execute CMD commands through programs


In CMD, typing below command will open the file c:/myFile.txt >

C:\> c:\myFile.txt


And we can write java program which will execute the above CMD command.



Program to Execute CMD commands through java programs >

import java.io.IOException;
/** JavaMadeSoEasy.com */
public class ExecuteCMDprogramThroughJava {
   public static void main(String... args) {   
         
          try {
                 Runtime.getRuntime().exec("cmd.exe /c c:/myFile.txt");
                 System.out.println("CMD commands has been executed through java program.");
          } catch (IOException e) {
                 e.printStackTrace();
          }
      
   }
}
/* OUTPUT
CMD commands has been executed through java program.
*/




RELATED LINKS>

Program to Hide and unHide File or Directory (by executing CMD commands in java program) till java6 - file IO


Read text entered by user in console>

Program to Read text entered by user in console using DataInputStream in java file IO

Read text entered by use in consoler using BufferedReader's readLine() method in java file IO


No comments:

Post a Comment