I am using the following code to run the cmd.exe and the cmd window appears which is fine.
Runtime runtime = Runtime.getRuntime();
try {
Process p = runtime.exec("cmd.exe /c start");
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
My question is that how can I run the following command on the cmd before it appears using java
ghci test.hs
my target is to make the command line looks like the following image once it appears
-
1Do you need the cmd for path or something? You could call the ghci.exe or ghci.com directly with params I guess?Daniel Persson– Daniel Persson2015年12月12日 11:37:00 +00:00Commented Dec 12, 2015 at 11:37
-
@DanielPersson I tried to call ghci earlier it works, however the cmd window does not appear.Haskell00– Haskell002015年12月12日 11:42:00 +00:00Commented Dec 12, 2015 at 11:42
-
so you want to run cmd /c ghci.exe test.ha?Daniel Persson– Daniel Persson2015年12月12日 11:43:00 +00:00Commented Dec 12, 2015 at 11:43
-
/c stands for command.Daniel Persson– Daniel Persson2015年12月12日 11:43:21 +00:00Commented Dec 12, 2015 at 11:43
-
@DanielPersson yes exactly and appear the command line window image number 2Haskell00– Haskell002015年12月12日 11:53:08 +00:00Commented Dec 12, 2015 at 11:53
1 Answer 1
run
Runtime runtime = Runtime.getRuntime();
try {
Process p = runtime.exec("start cmd.exe /k \"ghci.exe test.hs\"");
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
referens http://ss64.com/nt/cmd.html
more info at How to open the command prompt and insert commands using Java?
answered Dec 12, 2015 at 11:44
Daniel Persson
6027 silver badges17 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Daniel Persson
try adding start before cmd
Haskell00
adding start caused this error : Dec 12, 2015 3:16:20 PM ui.Main Programmar1ActionPerformed SEVERE: null java.io.IOException: Cannot run program "start": CreateProcess error=2, The system cannot find the file specified
Daniel Persson
@haskell00 try adding start before
Haskell00
Thanks a lot, you gave me a clue to my problem. I solved the problem using this command Process p = runtime.exec("cmd.exe /c cd \""+ "C:\\Users\\WIN8\\Desktop\\test" +"\" & start cmd.exe /k \"ghci test.hs\"");
lang-java