views:

187

answers:

2

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?

A: 

I'm not 100% certain this is what you're after but I think what you want is javaw instead of java.

Taken from the Sun JDK 6 documentation:

The javaw command is identical to java, except that with javaw there is no associated console window. Use javaw when you don't want a command prompt window to appear. The javaw launcher will, however, display a dialog box with error information if a launch fails for some reason.

John Munsch
+2  A: 

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.

Bob Cross
Thanks, that was exactly the answer I needed. I'm using nant, not ant, but it has the same option. I still think something's broken deeper down, since START shouldn't leave thangs danglin', but I'm not going to argue with success! Now if only the nant doc was as clear as the ant doc!
Ross Patterson
@Ross, right, I forgot. The reality is that the tasks are pretty much the same. However, just to fit the topic, I'll edit my answer to point at the NANT website instead of ANT.
Bob Cross