-2

I want to run batch file using Java. That batch file doesn't require any arguments. But on running it asks to press enter key twice.

How can I do this using Java code?

This is what I have tried in batch. If I can do this in batch that will also work instead of Java

@if (@CodeSection == @Batch) @then

@echo off

rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=C:\Windows\System32\CScript //nologo //E:JScript "%~F0"

rem Start the other program in the same Window
start "" /B test.bat

%SendKeys% "{ENTER}"
%SendKeys% "exit{ENTER}"
goto :EOF

@end

// JScript section

var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));

This test.bat run for some time then asks for key press.

Mofi
  • 46,139
  • 17
  • 80
  • 143

1 Answers1

0

To run a batch file in java

import java.lang.Runtime;

Process run = Runtime.getRuntime().exec("cmd.exe", "/c", "path of the bat file");

According to your question you want to press enter key twice, to do this you can add code in the batch file to enter whenever required

Example

WScript.CreateObject("WScript.Shell").SendKeys("{ENTER}");

For more details How to press a key with batch file

Community
  • 1
  • 1
Ravikumar
  • 891
  • 12
  • 22