Need Help to Execute the commands from Java Program
Can anybody help me how to execute the following one
import java.io.*;
class RunCommandDemo
{
public static void main(String args[])
{
try
{
String s=null;
//System.out.println(args[0]);
String arg[]={"type","D:/Anji/JavaExamples/MyFile.txt"};
Process p=Runtime.getRuntime().exec(arg);
BufferedReader bf=new BufferedReader(new InputStreamReader(p.getInputStream()));
while((s=bf.readLine())!=null)
System.out.println(s);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
on execution i'm getting the following error:-
------------------------------------------
D:\Anji\JavaExamples>java RunCommandDemo
java.io.IOException: Cannot run program "type": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at RunCommandDemo.main(RunCommandDemo.java:11)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 4 more
Note:-
----
I tried it by reading from the command line also But i couldn't get the result.