0
votes

The below command works fine when i run it from unix box, but when I execute it from a tcl script its not working. please help me how to execute the command.

java -jar diffkit-0.9.0/diffkit-app.jar -planfiles plan.xml

I have tried the following lines of code

exec "java -jar diffkit-0.9.0/diffkit-app.jar -planfiles plan.xml"

eval "java -jar diffkit-0.9.0/diffkit-app.jar -planfiles plan.xml"
2
Please add the error that you get the next time too. - Johannes Kuhn
Actually, it works fine when you run it from a UNIX shell. The shell splits the string into words. If you for some reason need the services of a shell for starting java, use e.g. exec sh -c "java -jar diffkit-0.9.0/diffkit-app.jar -planfiles plan.xml", otherwise see Johannes answer. - potrzebie

2 Answers

5
votes

Use separate arguments.

exec java -jar diffkit-0.9.0/diffkit-app.jar -planfiles plan.xml

I'm not sure if exec looks in the path, so it is better do do the following:

exec {*}[auto_execok java] diffkit-0.9.0/diffkit-app.jar -planfiles plan.xml

Or if you are on 8.4 (which is end of life by the way)

eval [linsert {exec diffkit-0.9.0/diffkit-app.jar -planfiles plan.xml} 1 [auto_execok java]]
1
votes

There are three ways to do it:

exec java -jar diffkit-0.9.0/diffkit-app.jar -planfiles plan.xml

No quotes; each Tcl word becomes a separate argument.

set command "java -jar diffkit-0.9.0/diffkit-app.jar -planfiles plan.xml"
exec {*}$command
# or in 8.4 and before, one of these:
# eval exec $command
# eval [list exec] [lrange $command 0 end]
# eval [linsert $command 0 exec]

Or, if you prefer shell syntax to Tcl syntax:

set command "java -jar diffkit-0.9.0/diffkit-app.jar -planfiles plan.xml"
exec /bin/sh -c $command

This last one is very useful when you need to do complex redirections; they're currently easier to do in the Bourne shell (and its derivatives) than in Tcl. It (probably) won't work on Windows though; the equivalent with firing stuff through CMD.EXE is unfortunately a bit horrible.