繁体   English   中英

我如何输入从批处理文件,Java应用程序运行的命令提示符

[英]How do I input into a command prompt, that's ran from a batch file, from a java application

我想知道是否有一种方法可以从Java程序从已执行的批处理文件运行到命令提示符中。 例如 我有一个计时器,该计时器开始于9 AM,结束于9 PM。我可以启动批处理,但是当我想要停止它时,我无法获得“停止”来进入命令提示符以启动批处理文件命令。 请帮忙,谢谢!

public class Main extends Voids{
String start = "09:00:00 AM";
String end = "09:00:00 PM";
public Main()
{
    try
    {
        int x = 1;
        while(x == 1)
        {
            x = 2;
            if(x == 2)
            {
                SimpleDateFormat ft = new SimpleDateFormat ("hh:mm:ss a");
                Date date = new Date();
                System.out.println(ft.format(date));
                Thread.sleep(1000);
                if(ft.format(date).equals(stime))
                {
                    Runtime.getRuntime().exec("cmd /c start start.bat");
                    Thread.sleep(43200000);
                    try{
                        Runtime runtime = Runtime.getRuntime();
                        Process process = runtime.exec("stop"); // you might need the full path
                        InputStream is = process.getInputStream();
                        InputStreamReader isr = new InputStreamReader(is);
                        BufferedReader br = new BufferedReader(isr);
                        String line;

                        while ((line = br.readLine()) != null) {
                            System.out.println(line);
                        }
                        }catch(Exception e)
                        {
                            e.getStackTrace();
                        }
                }else
                {
                    x = 1;
                }
            }
        }
    }catch(Exception e)
    {
        System.out.println("EXCEPTION: \n");
        e.getStackTrace();
    }
}

批量:

@ECHO off
cls
:start
ECHO Enter "stop" to end session
set /p choice=:
if '%choice%'=='' ECHO "%choice%" is not valid please try again
if '%choice%'=='stop' goto stop
ECHO.
goto start
:stop
ECHO STOPPING SESSION
pause
exit

您已经有一个InputStream 现在以类似的方式获取OutputStream ,并简单地对其进行写入。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM