4

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.

I've found a topic where the Runtime class is used: http://www.javaquery.com/2011/03/how-to-execute-multiple-command-in.html

However it doesn't launch a real cmd.exe window, it's only run in background and the output is just printed on the Eclipse console.

I'm looking for a solution to run a real cmd.exe window and I need to pass as many commands as I want to that windows shell. Is this possible?

4

3 Answers 3

8

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\"");
3
  • Your solution works great for Windows command indeed thanks ! However when I run this : .exec("cmd /c start cmd.exe /K \"C:/cygwin64/bin/bash.exe && ls\""); It does executes the cygwin terminal inside cmd.exe but it doens't execute the ls command inside cygwin terminal ? Why is that ?
    – singe3
    Jun 25, 2014 at 11:12
  • it opens a cmd.exe windows, then displays "/usr/bin/bash: /K No such file or directory" and closes it immediatly after
    – singe3
    Jun 25, 2014 at 11:29
  • I found the solution thanks to part of your code and the bash manual : .getRuntime().exec("cmd /c start C:/cygwin64/bin/bash.exe --login -c \"ls ; whoami ; exec bash\""); Thanks ! Can you edit your post with that line of code please ?
    – singe3
    Jun 25, 2014 at 12:04
1

If you do not need to show a console on the screen, that is easy. You have some simple steps to follow :

  • start a Process via `Process cmd = new ProcessBuilder("cmd.exe").start();
  • send your commands to cmd.getOutputStream()
  • read the result of the commands from cmd.getInputStream() and/or cmd.getErrorStream()
  • when finished with it close cmd.getOutputStream(), and if necessary kill the process by cmd.destroy()

Optionnaly, you can have output and error stream to be merged :

Process cmd = new ProcessBuilder("cmd.exe").redirectErrorStream(true).start();

then you simply ignore cmd.getErrorStream() and only read from cmd.getInputStream()

1
  • Actually, as mentionned I need to show the console, but thanks it can be useful in the future
    – singe3
    Jun 25, 2014 at 10:57
0

Not quite sure , but if i properly understand your problem , try : for windows at java conifguration panel, there should be un-ticked the show console button.

3
  • This can be a comment
    – AJJ
    Jun 25, 2014 at 10:12
  • I didn't found the checkbox you're refering to, but I think it's not relevant to my issue because I don't want to see an Eclipse console but the actual Windows cmd prompt
    – singe3
    Jun 25, 2014 at 11:03
  • not at eclipse configurations. type at your windows search java config
    – AntJavaDev
    Jun 26, 2014 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.