I'm trying to excecute a command in dos from within my Java program, something like ("java " + commandline), but I can't get it to work.
This lcode is in Driver.java:
And this is my Cat.java, it prints the contents of the file(s) passed to it as arguments:
It looks like the command line is formatted correctly, but nothing happens upon execution...any help would be greatly appreciated.
This lcode is in Driver.java:
Code:
String commandline = "Cat file.txt";
Runtime rt1 = Runtime.getRuntime();
Process pr1 = rt1.exec("java " + commandline);
And this is my Cat.java, it prints the contents of the file(s) passed to it as arguments:
Code:
import java.io.*;
public class Cat {
public static void main(String argv[]) {
int counter = 0;
while(counter < argv.length)
{
FileInputStream fin1;
BufferedInputStream bin2;
try {
fin1 = new FileInputStream(argv[counter]);
}
catch (FileNotFoundException f) {
throw new Error("The system cannot find the file " + argv[0]);
}
bin2 = new BufferedInputStream(fin1);
byte[] bytes;
boolean end;
try {
do {
int avail = bin2.available();
end = (avail == 0);
bytes = new byte[avail];
bin2.read(bytes);
System.out.print(new String(bytes));
}
while (!end);
bin2.close();
System.out.println("");
}
catch (IOException ex) {
throw new Error("IO Error");
}
counter++;
}
}
}
It looks like the command line is formatted correctly, but nothing happens upon execution...any help would be greatly appreciated.