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?

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
singe3
  • 2,065
  • 4
  • 30
  • 48
  • this might help you.. http://stackoverflow.com/questions/4688123/how-to-open-the-command-prompt-and-insert-commands-using-java – AJJ Jun 25 '14 at 10:06
  • So you need a command line? – Stefan Jun 25 '14 at 10:15
  • possible duplicate of [How to execute cmd commands via Java](http://stackoverflow.com/questions/4157303/how-to-execute-cmd-commands-via-java) – nobody Jun 25 '14 at 13:23
  • yes duplicate question, but the solution given in that topic wasn't working for me – singe3 Jun 25 '14 at 14:09

3 Answers3

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\"");
Community
  • 1
  • 1
AJJ
  • 3,570
  • 7
  • 43
  • 76
  • 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 '14 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 '14 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 '14 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()

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Actually, as mentionned I need to show the console, but thanks it can be useful in the future – singe3 Jun 25 '14 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.

AntJavaDev
  • 1,204
  • 1
  • 18
  • 24