开发者

Send userinp to batch from Java

开发者 https://www.devze.com 2023-03-13 08:27 出处:网络
I have batch.cmd and I am running it in java by process = Runtime.getRuntime().exec(\"batch.cmd\"); and I do not know how can I enter (1-6).

I have batch.cmd and I am running it in java by

process = Runtime.getRuntime().exec("batch.cmd");

and I do not know how can I enter (1-6).

I tried: Console console = System.console(); but console is null

or

OutputStream outputStream = process.getOutputStream(); pos.write(49); //1 nothing happend

Could anyone help?

-----batch.cmd-----
echo start
:start
cls
set /p userinp=choose a number(1-6):
set userinp=%userinp:~0,1%
if "%userinp%"=="1" goto 1
if "%userinp%"=="2" goto 2
if "%userinp%"=="3" goto 3
if "%userinp%"=="4" goto 4
if "%userinp%"=="5" goto 5
if "%userinp%"=="6" goto 6
echo invalid choice
g开发者_开发知识库oto start
:1
echo 1
goto end
:2
echo 2
goto end
:3
echo 3
goto end
:end
pause>nul


Try flushing the output stream.


Try:

BufferedWriter out 
    = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
out.write(option + "\n" );
out.close();


If you have full access to the source code of the cmd file, then the most obvious solution is to not use SET /P but to pass the option directly as a parameter to the batch file.

Change batch.cmd to ....

echo start
set inp=%1
if "%inp%"=="1" goto 1
if "%inp%"=="2" goto 2
echo invalid parameter
goto :eof
:1
echo 1
goto :eof
:2
echo 2
goto :eof

and invoke it with a single parameter of choice.

0

精彩评论

暂无评论...
验证码 换一张
取 消