0

I am trying to run a batch file using java. The batch file in turn runs a python program. So i should wait till the batch file is done and then proceed with my program.

Problems facing:

  1. I could not run batch file in background. I am able to run it only via start

    Process p = Runtime.getRuntime().exec("cmd /c start c://GCTI//IA/QAART//testercheck.bat");

  2. once the batch file ran, it is not closing automatically.

Batch file

"C:\Python27\python.exe" -i "C:\GCTI\IA\QAART\tester\test_monitor.py" -init "C:\GCTI\IA\EpiPhone\Dispatcher6\init\INIT_Designer_QAART_Dispatcher_Chat.PY" -testlist "C:\GCTI\IA\ASR_QAART\dat files\ChatAutomation\chat.dat" 23

Can you please help me to run this batch ile in background?

  • Hello @AarthiKannan! Welcome to StackOverflow! Does this answer your question? https://stackoverflow.com/questions/615948/how-do-i-run-a-batch-file-from-my-java-application – William Martens Mar 05 '21 at 14:18
  • 1
    Hi Martens , I need to run this in my background. But when using start the cmd window is opening which is disturbing the ui automation. So i need to run this bat in background once it is done i should be able to proceed forward. – Aarthi Kannan Mar 05 '21 at 14:21
  • what is the problem that you're facing? (It is e.g a error code? or, unexpected behavior - if you try to run it in background?) Edit: the bat file - (if this is possible in your bat file) - can't it have some exit at the end of it? – William Martens Mar 05 '21 at 14:23
  • when i manually run this using cmd, the application is running and it is not exiting. Fatal Error - YES User Error - 1 All tests - 1 Skipped - 0 Missed - 0 Executed - 1 Failed - 1 Passed - 0 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ >>> so where can i mention in bat file to exit? – Aarthi Kannan Mar 05 '21 at 14:27
  • can you maybe just try to use `Runtime.getRuntime().exec("batfile.bat");` ? Just to see if that works, or not. – William Martens Mar 05 '21 at 14:33
  • try { Process p = Runtime.getRuntime().exec("c://GCTI//IA/QAART//testercheck.bat"); BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream())); StringBuilder sb=new StringBuilder(); String line; while((line=br.readLine())!=null) { sb.append(line); } int returnCode=p.waitFor(); if(returnCode==0) System.out.println(sb); else System.out.println(("Not Successfull")); } catch (IOException ex) { System.out.println(ex); } This is my program, the application is working but it's getting stuck in waitFor() method – Aarthi Kannan Mar 05 '21 at 14:39
  • Hi Aarthi, I wonder if the `-i` makes the Python session to stay open. The Python help says that `-i` will "inspect interactively after running script; forces a prompt even if stdin does not appear to be a terminal;". Experiment to see if you can run Python without `-i`. Just an idea. Good luck. – devdanke Mar 06 '21 at 05:44

1 Answers1

1

You don't need the batch file. You can execute the Python program directly from Java code using class java.lang.ProcessBuilder.

ProcessBuilder pb = new ProcessBuilder("C:\\Python27\\python.exe",
                                       "-i",
                                       "C:\\GCTI\\IA\QAART\\tester\\test_monitor.py",
                                       "-init",
                                       "C:\\GCTI\\IA\\EpiPhone\\Dispatcher6\\init\\INIT_Designer_QAART_Dispatcher_Chat.PY",
                                       "-testlist",
                                       "C:\\GCTI\\IA\\ASR_QAART\\dat files\\ChatAutomation\\chat.dat",
                                       "23");
Process p = pb.start();
int result = p.waitFor();

Refer to other methods in class ProcessBuilder for handling the output of the Python script, for example method inheritIO

Abra
  • 19,142
  • 7
  • 29
  • 41