从Java写入Windows CMD [英] Writing to Windows CMD from Java

查看:190
本文介绍了从Java写入Windows CMD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Java在Windows的命令提示符中输入命令。我使用processBuilder,打开命令提示符,并获取输出流,但是当我尝试对此进行写操作时,似乎什么都没有发生。我是否需要包含其他内容,还是我要解决所有这些错误?

I'm trying to be able to input commands into the command prompt on Windows from Java. I use processBuilder, open the command prompt, and get the output stream, but when I try to write to that, nothing seems to happen. Do I need to include something else, or am I going about this all wrong?

我知道我可以在初次启动时将包含命令的参数传递到命令提示符中,但我的目标是能够先打开窗口 ,然后再第二与之交互。

I know that I can pass arguments including commands into the command prompt when I initially start it, but my goal is to be able to open the window first, then interact with it second, not at the same time.

我的代码:

    ArrayList<String> commands = new ArrayList<>();
    commands.add("cmd.exe");
    commands.add("/c");
    commands.add("start");

    ProcessBuilder pb = new ProcessBuilder(commands);

    Process p = pb.start();

    BufferedWriter stdin = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));

    stdin.write("dir");
    stdin.newLine();
    stdin.flush();

我尝试搜索,但没有找到满意的答案。这段代码正是我从搜索中整理出来的。
我的最终目标是能够为youtube-dl(命令行程序)编写用户界面,以便使我对这些事情有更多的经验。我希望用户能够从多个选项中进行选择,然后根据他们的选择执行程序。

I've tried searching, but I haven't found any satisfactory answers. This code is what I've managed to piece together from that search. My end goal is to be able to write a user interface for youtube-dl, a command line program, so I can get more experienced with such things. I'd like the user to be able to pick from several options, then execute the program based on their selection.

推荐答案

I试图手动执行youtube-dl事情,因为youtube dl事情是使用大量CMD命令的自动脚本。

I was trying to do manually the youtube-dl thing, since youtube dl thing is an automatic script that uses a lot of CMD Commands.

返回主题,使用:

Runtime.getRuntime().exec()

这篇关于从Java写入Windows CMD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆