How to use the exec binding with Windows 10

I am just pursuing if the command maybe is wrong. Maybe you need to escape the backslashes.
Yes, you can get a returnvalue

logInfo(executeCommandLine(Duration.ofSeconds(5),“C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe”)

I have tried it this way:

response = executeCommandLine(Duration.ofSeconds(20),"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe")

In the log file I get a timeout warning for the command and the respone variable has the value null.

I made the test simpler:

    response = executeCommandLine("echo hi there")
    logInfo("Test ON", response)

this lead to the following warning in the openhab.log:

2023-08-24 18:25:58.685 [WARN ] [rg.openhab.core.io.net.exec.ExecUtil] - Error occurred when executing commandLine '[echo hi there]'
java.io.IOException: Cannot run program "echo hi there": CreateProcess error=2, Das System kann die angegebene Datei nicht finden

response value = null

echo hi there works fine entered manually in cmd.

Is there any way to make a bomb proof test?

I am running openHAB as a service.

Neither executeCommandLine nor the Exec binding are designed to kick off long running processes. both expect what ever program you run starts, does its thing, and then exits with some meaning output printed to the output.

Chrome doesn’t do that. It starts and stays running until you close it. Eventually OH will kill the process when it times out.

There’s also a potential issue with starting a program with a GUI from openHAB. There might be security controls in place to prevent that. I know that’s the case for Linux.

I think the whitespaces in the command could still make some problems.
Let‘s start with a bullet proof test.
Create a .bat file e.g. in your scripts folder with this command

dir > c:\path to your script file\output.txt

and call this command through executecommand line.
Eventually you have to start command.com and pass the batch file as an argument
If that works then replace dir with google command.

There is a long discussion in this thread:

I created a test.bat. If I start it manually it writes the output.txt.
Started with executecommandline it does nothing.
I dont know excatlly how to start it with command.com.

Did you try

logInfo(executeCommandLine(Duration.ofSeconds(5), "C:\\path to bat\\test.bat")

Just like to know if it works.

logInfo(executeCommandLine(Duration.ofSeconds(5), "command.com" "/c" "start" "\"\"" "C:\\path to bat\\test.bat")

This is how you start a new command.com instance which runs the batch file.

logInfo(executeCommandLine(Duration.ofSeconds(5),"C:\\test.bat")) 

leads to:

2023-08-24 21:59:00.869 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'Test-1' failed: An error occurred during the script execution: index=1, size=1 in Test
logInfo(executeCommandLine(Duration.ofSeconds(5),"command.com" "/c" "start" "\"\"" "C:\\test.bat"))

seams to have wrong syntax:

2023-08-24 22:04:33.871 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'Test.rules' has errors, therefore ignoring it: [13,68]: mismatched input '"/c"' expecting ')'[13,102]: mismatched input ')' expecting 'end'

I had the same problem. Look here:

In command

I would expect to see commas between the arguments.

So if you solved it for the exec binding why don’t you use it ?

Yes. My fault. Maybe also replacing „/c“ with „\/c“

Would that be the right syntax?

logInfo(executeCommandLine(Duration.ofSeconds(5),"command.com","/c","start","\"\"","C:\\test.bat"))

I am not familiar anymore with DOS commands.

I used winthing (no serice) to restart OH and IOTlink (service) to start command on my other computers.
IOTLink has the same windows service restrictions as OH running as a service. Winthing and IOTLink are not supported any more. So after solving the service restriction problem I wanted to give the exec binding a try not knowing the problem it cuases on windows.

Why is this not running?

not sure if the syntax is correct for logging, so better split it. I am not familiar anymore with DSL

var return = executeCommandLine …
logInfo ("test", return)

Please always provide any kind of results, logs etc otherwise we cannot help

OK this works:

var response = executeCommandLine(Duration.ofSeconds(5),"C:\\test.bat")
logInfo("Test ON", response)

This does not:

var response
rule "Test ON"
when
Item Test changed from OFF to ON
then 
response = executeCommandLine(Duration.ofSeconds(5),"C:\\test.bat")
logInfo("Test ON", response)

Is this logic or syntax …?

Syntax. You haven’t declared return as a variable. It needs to be

var response = ...

Ok. Now we have a starting point. test.bat is executed and output.txt is generated? Did you make sure it was not an old one from one of your tests from command line interface?

If you start chrome.exe from within your .bat file then we face the next problem that it is not loaded. Then you can try your solution by deactivating UAC

UAC is off.

    var response = executeCommandLine(Duration.ofSeconds(5),"echo Hallo")
    logInfo("Test ON", response)

leads to:

2023-08-25 09:52:30.314 [WARN ] [rg.openhab.core.io.net.exec.ExecUtil] - Failed to execute commandLine '[echo Hallo]'
2023-08-25 09:55:48.989 [INFO ] [rg.openhab.core.model.script.Test ON] - null

echo Hallo > c:\output.txt in the test.bat file works.

put “echo hallo” into your test.bat.
or try

var response = executeCommandLine(Duration.ofSeconds(5),"echo", "Hallo")

If this does not work you need to work with command.com as echo is an internal command