I'm trying to open the file using Runtime. This is similar to opening windows command prompt, and then executing the command.
Here is the code:
import java.io.IOException;
public class OpenFile {
public static void main(String[] args) {
String fileName = "E:\\Myfile.txt";
try {
Runtime rt = Runtime.getRuntime();
rt.exec(new String[]{"cmd.exe", "/c", "start"});
rt.exec(new String[]{fileName});
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
The command prompt is opening successfully. But the file Myfile.txt is not opening. I'm getting the below error in console:
java.io.IOException: CreateProcess: E:\Myfile.txt error=193
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
at java.lang.Runtime.exec(Runtime.java:591)
at java.lang.Runtime.exec(Runtime.java:464)
at OpenFile.main(OpenFile.java:10)
How to open the file successfully?