Running windows 10 ubuntu bash commands from java [closed]

Solution 1:

Assuming bash is on your path, you can run the following cmd command run a bash one:

bash -c "<your command>"

Just combine this with the normal java way of running a command:

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(String.format("bash -c \"%s\"", <your command>));

Solution 2:

You assume that

the only way for me to run UNIX commands is to download ubuntu bash for windows 10

, but it's not correct.

A rough search with the keyword "run linux command in windows" leads us to various solutions, above all, two, as stated here:

  1. Run Bash Shell from Windows 10
  2. Install Cygwin.

Actually, I have a third way: install git for Windows to enable Bash in Windows. As stated here. This approach may be more limited to Cygwin, I haven't test it.

I have installed both Cygwin x64 and git without conflict, so you can just use them in cmd console. I think your question may change into "How can I run Bash command in Java in Windows if the Bash is enabled?"

Now, I don't know if you have been aware of how to run Bash commands from Java. If so, you are done here. If not, ask Google.


A little more search before asking here is always helpful. Google is smarter than we think, and most of the time, even smarter than most of us, may we admit it or not.