gpt4 book ai didi

java - 从 java 程序运行 MS-DOS 命令

转载 作者:行者123 更新时间:2023-12-01 06:58:31 31 4
gpt4 key购买 nike

如何在我的 java 程序中运行 MS-DOS 命令?

最佳答案

How to run command-line or execute external application from Java :

import java.io.*;

public class Main {

public static void main(String args[]) {

try {
Runtime rt = Runtime.getRuntime();
//Process pr = rt.exec("cmd /c dir");
Process pr = rt.exec("c:\\helloworld.exe");

BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

String line=null;

while((line=input.readLine()) != null) {
System.out.println(line);
}

int exitVal = pr.waitFor();
System.out.println("Exited with error code "+exitVal);

} catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
}

关于java - 从 java 程序运行 MS-DOS 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5053766/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com