I want to run code compiled before. I compiled anyway it is not important how to compile but running the code is problem.
My code.java
public class code{
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
Then I compiled this code and code.class(in the D:// directory) was generated. Now I want to run this compiled file. My code is :
import java.io.IOException;
import java.io.InputStream;
public class compiler {
public static void main(String[] args) {
final String dosCommand = "cmd /c java code";
final String location = "D:\\";
try {
final Process process = Runtime.getRuntime().exec(
dosCommand + " " + location);
final InputStream in = process.getInputStream();
int ch;
while((ch = in.read()) != -1) {
System.out.print((char)ch);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here there is no error but this code does not do anything. No cmd opened, nothing. Where am I wrong? What should I do?
Runtime.exec(), use aProcessBuilder.