2013-11-21 42 views
2

我已經看到了幾個關於這個主題,但我沒有得到我的工作。 我想要做的就是從java程序打開cmd.exe。如何在java中打開cmd.exe

notepad.exe打開罰款。

的問題是,cmd.exe的dosent開放,代碼編譯好,並沒有錯誤

這裏是我的代碼:

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()); 

    } 
} 
} 
+2

你的問題不能被發現。請重試。 – Maroun

+1

有什麼問題?錯誤訊息?你有什麼嘗試? –

+0

可能重複[如何打開命令提示符並使用Java插入命令?](http://stackoverflow.com/questions/4688123/how-to-open-the-command-prompt-and-insert-commands-using -java) –

回答

7

試試這個..

public static void main(String args[]) { 
    try { 
     Runtime.getRuntime().exec("cmd.exe /c start"); 
     System.out.println("ok"); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
} 
+0

是的。這將起作用 – muthukumar

+0

提問者的代碼也可以工作。所以只要沒有問題描述,就不會有關於解決方案的知識。這裏的代碼可能會在提問者的機器上顯示相同的問題,我們根本不知道。 – Holger

+0

確定已打印,但cmd.exe不啓動或劑量得到顯示 – Daniel

1

至於建議很多這裏在SORuntime.getRuntime().exec(..)可能會給你帶來麻煩。而是使用ProcessBuilder API。

我用下面的:

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(); 
      } 
     } 
    } 

實用程序:

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 
    } 
} 
1

使用Java只有兩行代碼都需要打開CMD。

Runtime runtime = Runtime.getRuntime(); 
Process p = runtime.exec("cmd.exe /c start"); 

-------------->例如< ------------

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) { 
      } 
     } 
    }