在命令提示符中执行命令的 Java 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16757783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java application to execute commands in command prompt
提问by Mdlc
I would like to know, is there a way to let a java application open command prompt (cmd.exe) and type/execute commands, preferably without the user seeing the command prompt window.
我想知道,有没有办法让 Java 应用程序打开命令提示符(cmd.exe)并键入/执行命令,最好不要让用户看到命令提示符窗口。
If anyone knows a sample application or a piece of code that can do this, your answer will be appreciated!
如果有人知道可以执行此操作的示例应用程序或一段代码,您的回答将不胜感激!
回答by Juned Ahsan
The java.lang.ProcessBuilder and java.lang.Process classes are available for executing and communicating with external programs. With an instance of the java.lang.ProcessBuilder class, it can execute an external program and return an instance of a subclass of java.lang.Process. The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.
java.lang.ProcessBuilder 和 java.lang.Process 类可用于执行和与外部程序通信。使用 java.lang.ProcessBuilder 类的实例,它可以执行外部程序并返回 java.lang.Process 的子类的实例。Process 类提供了执行进程输入、执行输出到进程、等待进程完成、检查进程退出状态以及销毁(杀死)进程的方法。
public class ProcessBuildDemo {
public static void main(String [] args) throws IOException {
String[] command = {"CMD", "/C", "dir"};
ProcessBuilder probuilder = new ProcessBuilder( command );
//You can set up your work directory
probuilder.directory(new File("c:\xyzwsdemo"));
Process process = probuilder.start();
//Read out dir output
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:\n",
Arrays.toString(command));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
//Wait to get exit value
try {
int exitValue = process.waitFor();
System.out.println("\n\nExit Value is " + exitValue);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
回答by Alpesh Gediya
use below code to execute process using java.
使用下面的代码使用java执行进程。
final List<String> commands = new ArrayList<String>();
commands.add("cmd");
commands.add(input2); // second commandline argument
commands.add(input3);// third commandline agrument
ProcessBuilder pb = new ProcessBuilder(commands);
回答by stinepike
You can use Runtime.execmethod. For example following code will execute the "ipconfig" command and print the result
您可以使用Runtime.exec方法。例如下面的代码将执行“ipconfig”命令并打印结果
Process p = Runtime.getRuntime().exec("ipconfig");
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String readline;
while ((readline = reader.readLine()) != null) {
System.out.println(readline);
}
Or according to MadProgrammer's comment you can use ProcessBuilder
或者根据 MadProgrammer 的评论,您可以使用ProcessBuilder
ProcessBuilder pb = new ProcessBuilder("ipconfig");
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String readline;
while ((readline = reader.readLine()) != null) {
System.out.println(readline);
}
回答by Mike Q
It depends whether you need to run inside the shell (cmd.exe) or just run a program that you would normally run from the shell.
这取决于您是需要在 shell (cmd.exe) 内运行还是只运行通常从 shell 运行的程序。
If the former you need to invoke cmd -c <string>
using the appropriate Java APIs.
如果是前者,您需要cmd -c <string>
使用适当的 Java API进行调用。
If the latter you can use the Java APIs to invoke a process directly.
如果是后者,您可以使用 Java API 直接调用进程。
IMPORTANT:However there are many pitfalls with the Java Process APIs. You canuse java.lang.Process/ProcessBuilder
but see this linkto see the issues.
重要提示:但是,Java Process API 存在许多缺陷。您可以使用java.lang.Process/ProcessBuilder
但请参阅此链接以查看问题。
A better solution is to use Apache Commons Exec librarywhich solves all these problems for you.
更好的解决方案是使用Apache Commons Exec 库,它可以为您解决所有这些问题。