To be pedantic... The strings(s) that you are passing to be executed may
(are) not the parameters that are actually received (argv) by the program
that actually gets executed: executing 'echo' as the executable will show
you what actually made it to the executable program.

Enjoy,

Fred

On Tue, February 10, 2026 3:23 am, Alan Hewat wrote:
> *Thanks Rob.* Yes, the backslash \ is a character that escapes the
> following character. I used backslashes historically  but will try forward
> slashes with my current modern installation.
>
> *Fred, that's brilliant !!!!!!*
> exec("cmd /c start \"\" build.bat");
> Solves my problem ! I had already tried that, but without the double ""
> (escaped) which opens a new *persistent *untitled cmd window. My previous
> attempt, based on the examples in
> https://wsr.imagej.net/macros/ExecExamples.txt also opened a new cmd
> window
> but closed it immediately, and perhaps that was the problem. I use print()
> to check the strings I pass to exec().
>
> *Now, to be tidy, I just need to close that cmd window, after perhaps
> waiting a moment. *But I'm very happy that I can finally launch SharpCap
> with Python scripting from an imageJ script.
>
> Thanks also for the explanation of how exec() works. It was probably quite
> difficult to implement, having to pass parameters through several systems.
> It's perhaps still not quite perfect, which to my mind would mean that
> anything that worked in a Windows cmd shell or .bat file worked in exec,
> and that exec itself would parse the string. At least, the stated exec()
> requirement to use comma separated arguments when there are spaces in the
> cmd string, like
> exec("cmd", "/c", "start", "excel.exe", path);
> no longer seems necessary.
>
> Thanks again to all who responded, suggested new ideas, and solved my
> problem. Alan.
>
> On Tue, 10 Feb 2026 at 06:26, Fred Damen <[email protected]> wrote:
>
>> Greetings,
>>
>> A slightly more complicated explanation...
>>
>> ImageJ's macro function exec passes its parameters to JVM, which passes
>> its parameters to posix system call, which requests the OS to execute an
>> executable. At each stage things are manipulated. System shell commands
>> are not actually executables. You need to pass your shell command (.bat)
>> to an executable (cmd) to execute it.
>> exec("cmd /c start \"\" build.bat");
>> see:
>>
>> https://stackoverflow.com/questions/615948/how-do-i-run-a-batch-file-from-my-java-application
>> second answer.
>>
>> If you want to try and figure out what is going on and resolve the
>> issue,
>> try and execute the 'echo' command with your command as its parameters;
>> echo simply displays its parameters.  This will be frustrating and
>> enlightening, although it may help...
>>
>>
>> Fred
>>
>> On Mon, February 9, 2026 9:50 am, Alan Hewat wrote:
>> > Thanks Fred.
>> >
>> > Unfortunately *ImageJ exec() doesn't run Windows .bat files as
>> expected
>> > either :-(*
>> >
>> > I have a simple .bat file containing one line, that *executes without
>> > error
>> > when I double-click it in Windows*
>> > "C:\Program Files\SharpCap 4.1 (64 bit)\SharpCap.exe" /language=fr
>> > It launches SharpCap <https://docs.sharpcap.co.uk/4.1/>, a popular
>> imaging
>> > application for astronomers, with a command-line argument that
>> specifies
>> > the language
>> >
>> > If instead I execute it within an imageJ script with
>> > status = exec("C:\Program Files\SharpCap 4.1 (64 bit)\SharpCap.exe"
>> > /language=fr);
>> > It launches SharpCap but the in-line argument disables SharpCap's own
>> > IronPython script with a "codepage___0"  error.
>> > (It does work without the in-line argument, and I want to input more
>> > complicated arguments than the trivial /language).
>> >
>> > Other users have reported similar problems and it was suggested
>> > <https://forums.sharpcap.co.uk/viewtopic.php?t=6762>that the code page
>> or
>> > environment variables may be set differently.
>> > This problem occurs however I try to execute SharpCap with exec(),
>> e.g.
>> > https://wsr.imagej.net/macros/ExecExamples.txt
>> > There is a nice example
>> > <https://imagej.net/ij/macros/tools/VideoCaptureTool.txt> of using
>> exec()
>> > with command-line arguments to record and playback VirtualDub video
>> > That works for me, but doesn't help me understand my problem.
>> >
>> > *So is there any other way of launching a Windows process from an
>> imageJ
>> > macro other than exec() ?*
>> >
>> > Regards, Alan.
>> >
>> > On Mon, 9 Feb 2026 at 05:45, Fred Damen <[email protected]> wrote:
>> >
>> >> Greetings,
>> >>
>> >> Generally when I need to run a command line program from within a
>> >> program
>> >> I am writing and the passing of arguments becomes to frustrating (as
>> >> there
>> >> are many layers of retranslating the command syntax), I write a
>> command
>> >> line command in the OS(es) native macro language, .bat/.cmd in your
>> >> case.
>> >> Then the only thing that I need to get correct in the calling program
>> is
>> >> executing the OS macro command itself - which you seem to have
>> >> accomplished.
>> >>
>> >> Enjoy,
>> >>
>> >> Fred
>> >>
>> >>
>> >> On Sun, February 8, 2026 3:22 am, Alan Hewat wrote:
>> >> > SharpCap <https://docs.sharpcap.co.uk/4.1/> is a popular
>> astronomical
>> >> > imaging application and can be launched from a Windows cmd shell
>> with
>> >> > a command-line
>> >> > argument
>> >> <https://docs.sharpcap.co.uk/4.0/34_CommandLineArguments.htm>,
>> >> > like this:
>> >> > C:\Users\alanh>"..\\..\\Program Files\\SharpCap 4.1 (64
>> >> > bit)\\SharpCap.exe"
>> >> > /runscript d:\\cameraList.py
>> >> > This correctly launches SharpCap to run a Python script
>> cameraList.py
>> >> to
>> >> > list available cameras (or whatever else is requested).
>> >> >
>> >> > ImageJ has an exec() function
>> >> > <https://wsr.imagej.net/developer/macro/functions.html#E> that can
>> be
>> >> used
>> >> > to launch applications from an imageJ script. The following
>> launches
>> >> > SharpCap, but apparently doesn't pass the /runscript argument
>> >> correctly
>> >> > because SharpCap scripting reports an error and doesn't work.
>> >> > SharpCap = getDirectory("startup")+"..\\..\\Program Files\\SharpCap
>> >> 4.1
>> >> > (64
>> >> > bit)\\SharpCap.exe";
>> >> > status=exec (SharpCap+" /runscript d:\\cameraList.py");
>> >> >
>> >> > Guided by the exec() function
>> >> > <https://imagej.net/ij/macros/ExecExamples.txt> examples, I tried
>> >> instead:
>> >> > status=exec("cmd", "/c", "start", SharpCap, " /runscript", "
>> >> > d:\\cameraList.py" );
>> >> > but an error that "*Windows can't find /runscript*" is returned as
>> if
>> >> it
>> >> > thinks */runscript* is an application.
>> >> > Replacing */runscript* with the application *SharpCap* as below
>> works,
>> >> but
>> >> > I don't understand why
>> >> > status=exec("cmd", "/c", "start", SharpCap, SharpCap, "
>> /runscript", "
>> >> > d:\\cameraList.py" );
>> >> > That launches SharpCap, and no error is reported, but the script is
>> >> not
>> >> > executed.
>> >> >
>> >> > *The last attempt seems to be close to working, but I must have
>> made
>> >> some
>> >> > mistake in the exec() syntax.*
>> >> >
>> >> > The latest versions of imageJ and Win-11 were used. Any suggestions
>> >> would
>> >> > be appreciated. Alan.
>> >> >
>> >> >
>> >> > --
>> >> > *_______________________________*
>> >> > Dr Alan Hewat, NeutronOptics
>> >> > Grenoble, FRANCE (from computer)
>> >> > [email protected]
>> >> > +33.476984168 VAT:FR79499450856
>> >> > http://NeutronOptics.com/hewat <http://neutronoptics.com/hewat>
>> >> > _______________________________
>> >> >
>> >> > --
>> >> > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>> >> >
>> >>
>> >> --
>> >> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>> >>
>> >
>> >
>> > --
>> > *_______________________________*
>> > Dr Alan Hewat, NeutronOptics
>> > Grenoble, FRANCE (from computer)
>> > [email protected]
>> > +33.476984168 VAT:FR79499450856
>> > http://NeutronOptics.com/hewat <http://neutronoptics.com/hewat>
>> > _______________________________
>> >
>> > --
>> > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>> >
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>
>
> --
> *_______________________________*
> Dr Alan Hewat, NeutronOptics
> Grenoble, FRANCE (from computer)
> [email protected]
> +33.476984168 VAT:FR79499450856
> http://NeutronOptics.com/hewat <http://neutronoptics.com/hewat>
> _______________________________
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

Reply via email to