Search code examples
javawindowscommand-lineseleniumnant

Spawn a multi-threaded Java program from a Windows command line program, spawner won't end until spawnee ends. How can I prevent the hang?


Short version:

How can I prevent a spawned Java process in Windows from blocking the spawning process from ending?

Long version:

I'm trying to spawn a multi-threaded Java program (Selenium RC, not that it should matter) from a program launched from the Windows command line (NAnt's <exec> task, again, not that it should matter). I'm doing it using the Windows "start" command, and the spawned process is started and runs correctly. The spawning process receives control back and finishes (NAnt says "BUILD SUCCEEDED"), but doesn't actually exit to the command line. When the spawned process finally terminates (could be hours later), the command process returns and the command line prompt occurs.

For example:

<target name="start_rc">
    <exec program="cmd" failonerror="false" workingdir="${ross.p5.dir}\Tools\selenium\selenium-server-1.0.1" verbose="true">
        <arg value="/C"/>
        <arg value="start"/>
        <arg value="java"/>
        <arg value="-jar"/>
        <arg path="${ross.p5.dir}\Tools\selenium\selenium-server-1.0.1\selenium-server.jar"/>
        <arg value="-userExtensions"/>
        <arg path="${ross.p5.dir}\Tools\selenium\selenium-server-1.0.1\user-extensions.js"/>
        <arg value="-browserSideLog"/>
        <arg value="-log"/>
        <arg value="${ross.p5.dir}\artifacts\selenium.log"/>
        <arg value="-debug"/>
    </exec>
</target>

Produces:

C:\Ross>nant start_rc
NAnt 0.86 (Build 0.86.2898.0; beta1; 12/8/2007)
Copyright (C) 2001-2007 Gerry Shaw
http://nant.sourceforge.net

Buildfile: file:///C:/Ross/ross.build
Target framework: Microsoft .NET Framework 3.5
Target(s) specified: start_rc


start_rc:

     [exec] Starting 'cmd (/C start java -jar C:\p5\Tools\selenium\selenium-server-1.0.1\selenium-server.jar -userExtensions C:\p5\Tools\selenium\selenium-server-1.0.1\user-extensions.js -browserSideLog -log C:\p5\artifacts\selenium.log -debug)' in 'C:\p5\Tools\selenium\selenium-server-1.0.1'

BUILD SUCCEEDED

Total time: 4.1 seconds.

... and then nothing until I close the window where Java is running, then ...

C:\Ross>

Obviously something is preventing the nant process from terminating, but shouldn't the Windows START command prevent that?


Solution

  • You have a problem with your exec task in that spawn is defaulting to false. From the NAnt documentation:

    Gets or sets a value indicating whether the application should be spawned. If you spawn an application, its output will not be logged by NAnt. The default is false.

    In the Ant documentation (remember, NAnt is "Not Ant," but still...), there is a concrete example of the use of the spawn parameter of the exec task:

    <property name="browser" location="C:/Program Files/Internet Explorer/iexplore.exe"/>
    <property name="file" location="ant/docs/manual/index.html"/>
    
    <exec executable="${browser}" spawn="true">
        <arg value="${file}"/>
    </exec>
    

    When that browser task fires, Internet Explorer will open to that page but the build will continue without waiting for output or task completion.