如何在java中打开cmd.exe
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20120945/
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
How to open cmd.exe in java
提问by Daniel
I have seen several topics about this but i havent got i to work. All i am trying to do is open cmd.exe from a java program.
我已经看到了几个关于此的主题,但我还没有开始工作。我想要做的就是从 Java 程序打开 cmd.exe。
notepad.exe opens fine.
notepad.exe 打开正常。
The problem is that cmd.exe dosent open, the code compiles fine with no error
问题是 cmd.exe 没有打开,代码编译正常,没有错误
here is my code:
这是我的代码:
public class CMD {
public static void main(String[] args) {
//Trying some variants how to start.
//String cmd = "C:\WINDOWS\system32\cmd.exe";
//String[] cmd = {"C:\WINDOWS\system32\cmd.exe","start"};
String[] cmd = {"C:\WINDOWS\system32\cmd.exe","/c","start"};
// notepad works fine
//String notepad = "C:\WINDOWS\system32\notepad.exe";
try {
Runtime runtime = Runtime.getRuntime();
//Process p = runtime.exec(notepad);
Process p = runtime.exec(cmd);
}
catch (java.io.IOException exception) {
System.out.println("Caught IOException: " + exception.getMessage());
}
}
}
回答by subash
try this..
尝试这个..
public static void main(String args[]) {
try {
Runtime.getRuntime().exec("cmd.exe /c start");
System.out.println("ok");
} catch (IOException ex) {
ex.printStackTrace();
}
}
回答by phoenix
As suggested by many here on SO
, Runtime.getRuntime().exec(..)
might give troubles. Instead use ProcessBuilder
API.
正如这里的许多人所建议的那样SO
, Runtime.getRuntime().exec(..)
可能会带来麻烦。而是使用ProcessBuilder
API。
I used the following:
我使用了以下内容:
public static void run(String argument) throws IOException {
List<String> command = new ArrayList<String>();
OsCheck.OSType osType = OsCheck.getOperatingSystemType();
System.out.println("OS: " + osType);
String shell;
if(osType.toString().equals("Windows")) {
command.add("cmd.exe");
command.add("/c");
} else {
shell = "/bin/bash";
command.add(shell);
}
command.add(argument);
InputStream inputStream = null;
InputStream errorStream = null;
try {
ProcessBuilder processBuilder = new ProcessBuilder(command);
Process process = processBuilder.start();
inputStream = process.getInputStream();
errorStream = process.getErrorStream();
System.out.println("Process InputStream: " + IOUtils.toString(inputStream, "utf-8"));
System.out.println("Process ErrorStream: " + IOUtils.toString(errorStream, "utf-8"));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
inputStream .close();
}
if (errorStream != null) {
errorStream.close();
}
}
}
Utility:
公用事业:
public final class OsCheck {
/**
* Enum type which contains OS names.
*/
private static OSType detectedOS;
/**
* <p>
* Finds the OS
* </p>
*
* @return One of the values of the enum OSType
*/
public static OSType getOperatingSystemType() {
if (detectedOS == null) {
String OS = System.getProperty("os.name", "generic").toLowerCase();
if (OS.contains("win")) {
detectedOS = OSType.Windows;
} else if ((OS.contains("mac")) || (OS.contains("darwin"))) {
detectedOS = OSType.MacOS;
} else {
detectedOS = OSType.Linux;
}
}
return detectedOS;
}
/**
* Represents the popular os types i.e Windows, or MacOS or Linux
*/
public enum OSType {
Windows, MacOS, Linux
}
}
回答by Sushin Pv
To open cmd using java only two line codes are required.
使用 java 打开 cmd 只需要两行代码。
Runtime runtime = Runtime.getRuntime();
Process p = runtime.exec("cmd.exe /c start");
--------------> example <------------
--------------> 示例 <------------
public class Cmd {
public static void main(String[] args) {
try {
//Create the Process Instance, You don't need to Import anything for this.
Process p;
//To Execute the Process containg the Command as the Parameter
p = Runtime.getRuntime().exec("cmd /c start cmd");
//As the same way you can use all the commands to Execute your favorite apps.
} catch (Exception e) {
}
}
}