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.
notepad.exe opens fine.
The problem is that cmd.exe dosent open, the code compiles fine with no error
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());
}
}
}
asked Nov 21, 2013 at 12:24
Daniel
982 gold badges2 silver badges6 bronze badges
-
2Your question couldn't be found. Please retry.Maroun– Maroun2013年11月21日 12:24:41 +00:00Commented Nov 21, 2013 at 12:24
-
1What's the problem? Error messages? What have you tried?Irgendw Pointer– Irgendw Pointer2013年11月21日 12:25:55 +00:00Commented Nov 21, 2013 at 12:25
-
possible duplicate of How to open the command prompt and insert commands using Java?Irgendw Pointer– Irgendw Pointer2013年11月21日 12:26:52 +00:00Commented Nov 21, 2013 at 12:26
-
You have an exception handler that prints the exception and you won't paste that in your question?stackular– stackular2013年11月21日 12:31:00 +00:00Commented Nov 21, 2013 at 12:31
-
Hi, thanks for the response, the problem is that cmd.exe dosent open. nothing happens. The code is compiled with no error.Daniel– Daniel2013年11月21日 12:53:18 +00:00Commented Nov 21, 2013 at 12:53
3 Answers 3
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();
}
}
answered Nov 21, 2013 at 12:29
subash
3,1253 gold badges20 silver badges22 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Holger
The questioner’s code will work too. So as long as there’s no problem description there will be no knowledge about the solution. This code here might show the same problem on the questioner’s machine, we simply don’t know.
Daniel
OK is printed but cmd.exe doesnt start or dosent get displayed
subash
are you can open cmd in run Window key + R
As suggested by many here on SO, Runtime.getRuntime().exec(..) might give troubles. Instead use 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
}
}
answered Dec 2, 2014 at 5:04
phoenix
9953 gold badges19 silver badges40 bronze badges
Comments
To open cmd using java only two line codes are required.
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) {
}
}
}
ArtKorchagin
4,86913 gold badges45 silver badges58 bronze badges
Comments
lang-java