从user发送userinp到批处理

时间:2022-03-11 20:48:14

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

我有batch.cmd,我在java中运行它

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

process = Runtime.getRuntime()。exec(“batch.cmd”);

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

我不知道如何进入(1-6)。

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

我试过:Console console = System.console();但控制台为空

or

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

OutputStream outputStream = process.getOutputStream(); pos.write(49); // 1什么都没发生

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
goto start
:1
echo 1
goto end
:2
echo 2
goto end
:3
echo 3
goto end
:end
pause>nul

3 个解决方案

#1


0  

Try flushing the output stream.

尝试刷新输出流。

#2


0  

Try:

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

#3


0  

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.

如果您可以完全访问cmd文件的源代码,那么最明显的解决方案是不使用SET / P,而是将选项直接作为参数传递给批处理文件。

Change batch.cmd to ....

将batch.cmd更改为....

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.

并使用一个选择的参数调用它。

#1


0  

Try flushing the output stream.

尝试刷新输出流。

#2


0  

Try:

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

#3


0  

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.

如果您可以完全访问cmd文件的源代码,那么最明显的解决方案是不使用SET / P,而是将选项直接作为参数传递给批处理文件。

Change batch.cmd to ....

将batch.cmd更改为....

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.

并使用一个选择的参数调用它。