Start CMD by using ProcessBuilder

后端 未结 2 1081
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 22:04

I am trying to start the CMD application in windows by using the following code, but it doesn\'t work as expected. Several examples from different websites shows that \"cmd\

相关标签:
2条回答
  • 2020-12-01 22:19

    To use it with ProcessBuilder you must separate the commands like this:

    final List<String> commands = new ArrayList<String>();                
    
    commands.add("cmd.exe");
    commands.add("/C");
    commands.add("start");
    ProcessBuilder pb = new ProcessBuilder(commands);
    pb.start();
    
    0 讨论(0)
  • 2020-12-01 22:36

    You need to use the start command. Actually, even I don't see a new command prompt popping up, but you can check that a new cmd.exe is definitely started using your task manager.

    ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "start");
    

    Though, the same functionality can be achieved using Runtime.exec(), and this actually pops up a new command prompt.

    Runtime.getRuntime().exec("cmd.exe /C start");
    
    0 讨论(0)
提交回复
热议问题