从 Java 返回 Windows cmd 文本?

发布于 2024-12-13 14:19:54 字数 744 浏览 3 评论 0原文

我想要返回与在 Windows 中的 cmd 提示符中手动键入命令时返回的文本相同的文本。这是一个不起作用的示例。

import java.io.IOException;

public class Test {

    public static void main(String[] args) throws IOException {
        String g = "";
        Runtime.getRuntime().exec(new String[] {"ipconfig", g});  
        System.out.println(g);
    }
}

我不知道我是否应该研究 Runtime.getRuntime()exec 因为我理解 api 的方式 ( http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html ) 是所有的在 exec 示例中,没有一个返回字符串。有些返回值(如果我理解正确的话)实际上是一个“进程”,我只能猜测意味着没有返回任何内容,但该进程已启动。我在示例中使用了 ipconfig,但实际上我需要运行各种诊断命令并分析字符串(我将其称为“cmd 提示符”)。

I want to return the same text that is returned when I manually type a command into the cmd prompt in Windows. Here is an example that does not work.

import java.io.IOException;

public class Test {

    public static void main(String[] args) throws IOException {
        String g = "";
        Runtime.getRuntime().exec(new String[] {"ipconfig", g});  
        System.out.println(g);
    }
}

I don't know if I should be looking into Runtime.getRuntime()exec because the way I understand the api ( http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html ) is that of all of the exec examples, none return a string. The return value (if I understand right) on some is actually a 'process' which I can only guess means nothing gets returned, but the process is started. I used ipconfig in the example, but I actually need to run various diagnostic commands and analyze the string (which I have referred to as the 'cmd prompt').

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

林空鹿饮溪 2024-12-20 14:19:54

要捕获命令的输出,您可以使用以下命令:

Process p = Runtime.getRuntime().exec(new String[] {"ipconfig", g});
InputStream s = p.getInputStream();

BufferedReader in = new BufferedReader(new InputStreamReader(s));
String temp;

while ((temp = in.readLine()) != null) {
    System.out.println(temp);
}

请注意,readLine() 方法将阻塞,直到它读取输入或进程终止。

To capture the output of the command, you can use this:

Process p = Runtime.getRuntime().exec(new String[] {"ipconfig", g});
InputStream s = p.getInputStream();

BufferedReader in = new BufferedReader(new InputStreamReader(s));
String temp;

while ((temp = in.readLine()) != null) {
    System.out.println(temp);
}

Please not that the readLine() method will block until it reads an input or the process is terminated.

梦途 2024-12-20 14:19:54

Java 字符串是不可变的,这意味着 g 引用的 "" 永远不会改变。不执行 g 赋值的代码都不会向 System.out 打印空字符串以外的内容。

我建议您使用 Commons-Exec,而不是使用 Runtime.exec来自 Apache Commons 项目的库。它提供了一种更可靠的方式来执行外部应用程序(可靠地传递参数并防止诸如未读输出流锁定程序之类的情况)。

您可以使用 PumpStreamHandler< 捕获命令输出/code>和您选择的输入流。

The Java String is immutable, meaning that the "" referenced by g will never change. No code that doesn't do an assignment of g will ever print something other than the empty string to System.out.

Rather than using Runtime.exec, I recommend you use the Commons-Exec library from the Apache Commons project. It provides a much more reliable means of executing external applications (passing the arguments reliably and preventing such things as an unread output stream locking up the program).

You can capture the command output using the PumpStreamHandler and an input stream of your choice.

谢绝鈎搭 2024-12-20 14:19:54

如果您查看已经发布的 javadoc 链接,您将看到 Runtime.exec() 返回 Process 对象,并且 Process 类有一个方法 <一个href="http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html#getOutputStream%28%29" rel="nofollow">getOutputStream()< /code> 获取新进程的标准输出流。

If you look at the javadoc link you've already posted, you'll see that Runtime.exec() returns a Process object, and the Process class has a method getOutputStream() to get at the standard output stream of the new process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文