GeneralCommandLine: Quotes
Hi,
For some reason, behavior of this class confuses me all the time. Let say I'd like to get host mashine's IP on WiFi interface. The following command is used (with quotes escaped):
cmd /c "for /f "tokens=3" %a in ('netsh interface ip show address "Wi-Fi" ^| findstr "IP Address"') do @echo %a"
yeah, ugly... Anyway, I've tried with classic
Runtime.getRuntime().exec(<cmd>)
and it works. Now, what is the proper Open API way of running a such command? GeneralCommandLine is giving me all kind of errors, mostly not being able to get quoted params right. Like in this type of loop:
for /f "tokens=1" %a in ("foo") do @echo %a
Do I need to give up on long commands, and move it to a batch file? What would be an appropriate place to copy this batch file to? Some sort of plugin related temporary folder, maybe.
Thank you!
Please sign in to leave a comment.
The class was designed to shield its clients from thinking about quoting etc. - although it won't help much in your case. If I got it right, the following code should work:
Thanks, @Roman Shevchenko, but did you try it? What I'm trying to say, I can't make this command work with GeneralCommandLine at all. My guess is about the for loop with tokens, it doesn't like it for some reason.
Don't have Windows at hands at the moment.
Look, the class does not work with a single command string. Its goal is passing the arguments to a child process in exactly the same form. E.g., if you need to pass a couple of JSON strings, you give them to the class as-is, without any quoting and escaping - and the class ensures that a child process will receive these strings undamaged, with all the spaces and quotes in place.
In your example, I just don't know how many arguments cmd.exe expects.
How is that an answer? By looking at it's javadoc one would think that it's a legit abstraction over the Runtime.getRuntime().exec(<cmd>).
Even better version of it. And after having all my other commands done via GeneralCommandLine I just need to realize that it can handle only a subset of them? A subset where arguments come one by one? That's quite specific use-case for something called GeneralCommand. It also brings inconsistency into codebase, when you have to mix Runtime.exec() with ExecUtils and CommandLine. At least, if it's true and it's not meant for everything (honestly, I still want to believe that I'm just using it wrong), there must be a fallback constructor that would accept any command as is and would execute it as is.
I see possible solutions to this:
It's hard to tell what's wrong without actually trying it out. Any help is much appreciated.
Okay.