With Java, run multiple commands in the same cmd.exe window

后端 未结 3 883
名媛妹妹
名媛妹妹 2020-12-10 20:39

I\'m developing a Java application that will be run on a Windows computer occasionally. At some point I need to run a Cygwin prompt and performs some commands in it.

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-10 21:11

    This one works... using && operator you can add one or commands to be executed in same command prompt

    try {
        Process p = Runtime
                        .getRuntime()
                        .exec("cmd /c start cmd.exe /K \"dir && ping localhost && echo end\"");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    

    Consider the solution in here also

    Update from the questioner: Solution to execute commands in cygwin

    getRuntime().exec("cmd /c start C:/cygwin64/bin/bash.exe --login -c \"ls ; whoami ; exec bash\"");
    

提交回复
热议问题