0

I want to run a bat file used to compile sass to css from within a Kotlin program, on a Windows machine. I had everything working using Runtime.exec until I switched to a Windows account that had a space in the username. From what I read, I read that using ProcessBuilder would make this easier. It seems that even with ProcessBuilder I still can't get it to work, no matter what I try.

Here is my code so far

val commands = mutableListOf(
                "cmd",
                "/c",
                "C:\\Users\\John Doe\\VCS\\test\\tools\\sass\\windows\\dart-sass\\sass.bat",
                "--no-source-map",
                "C:\\Users\\John Doe\\VCS\\test\\src\\main\\sass\\global.scss",
                "global.css"
        )

val processBuilder = ProcessBuilder(commands)
val process = processBuilder.start()

...

The error I get is 'C:\Users\John' is not recognized as an internal or external command, operable program or batch file. It doesn't help if I surround the strings that have spaces with \".

  • Can you change the name on the account? Spaces in names are just evil. I find it strange that ProcessBuilder doesn't do the right thing given that you're passing unique arguments via a list. But I googled a bit, and that seems to be the case. It seems that on Windows, you have to put actual double quotes at the ends of the path values that contain spaces. Strangely, you have to do something else on Mac. Check this out: https://jvm-gaming.org/t/processbuilder-behaviour-on-different-platforms/35872 – CryptoFool Dec 03 '20 at 23:05

2 Answers2

0

If I remember correctly, all windows files and folders that have a space in the name have a matching short name in the old 8.3 format replacing additional space and other characters with a tilde (~) and a number.

So whatever is returning you the path for the .bat and .sscs files could return the full filename in that format?

Doesn't solve the problem but avoids it instead, I admit.

Also means you won't get busted when someone puts a space in the filename (OK, unlikely, but still something better to deal with from the start).

Consider something along the lines of the top 2 answers on this superuser thread

Aidanapword
  • 288
  • 1
  • 13
0

This is actually a Windows cmd issue. The question here shows that in cmd, in addition to quoting the file paths, you also have to quote the entire part of the command line text after the /c switch.

I don't know if this is the best way to do it in ProcessBuilder, but I was able to get it to work with the following code.

"cmd.exe",
                "/c",
                "\"\"C:/Users/John Doe/VCS/test/tools/sass/windows/dart-sass/sass.bat\" "
                + "--no-source-map "
                + "\"C:/Users/John Doe/VCS/test/src/main/sass/global.scss\" "
                + "\"global.css\"\""