0

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);
    }

image 1

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

imag2

Haskell00
  • 63
  • 1
  • 10

1 Answers1

2

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?

Community
  • 1
  • 1
Daniel Persson
  • 602
  • 7
  • 17
  • try adding start before cmd – Daniel Persson Dec 12 '15 at 12:13
  • 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 – Haskell00 Dec 12 '15 at 12:17
  • @haskell00 try adding start before – Daniel Persson Dec 12 '15 at 12:17
  • 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\""); – Haskell00 Dec 12 '15 at 12:23