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?

7
  • Hello @AarthiKannan! Welcome to StackOverflow! Does this answer your question? stackoverflow.com/questions/615948/… Commented Mar 5, 2021 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. Commented Mar 5, 2021 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? Commented Mar 5, 2021 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? Commented Mar 5, 2021 at 14:27
  • can you maybe just try to use Runtime.getRuntime().exec("batfile.bat"); ? Just to see if that works, or not. Commented Mar 5, 2021 at 14:33

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.