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
  • this might help you.. stackoverflow.com/questions/4688123/… Commented Jun 25, 2014 at 10:06
  • So you need a command line? Commented Jun 25, 2014 at 10:15
  • possible duplicate of How to execute cmd commands via Java Commented Jun 25, 2014 at 13:23
  • yes duplicate question, but the solution given in that topic wasn't working for me Commented Jun 25, 2014 at 14:09

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\"");
Sign up to request clarification or add additional context in comments.

3 Comments

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 ?
it opens a cmd.exe windows, then displays "/usr/bin/bash: /K No such file or directory" and closes it immediatly after
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 ?
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 Comment

Actually, as mentionned I need to show the console, but thanks it can be useful in the future
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 Comments

This can be a comment
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
not at eclipse configurations. type at your windows search java config

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.