Forking Processes
Q:
We need to
run a Java utility on some of our files during the build. When some
errors happen in this utility, we’ve noticed that
the build just dies. We get no success or fail messages, and the log
just abruptly ends. I think it may have something to do with the
System.exit( )
issue mentioned in Chapter 5, but we still don’t know how
to fix it. What do we do?
Ah yes, the System.exit( )
problem. To recap, the
problem is the result of developers’ misuse of the
System.exit( )
call in their code. The
System.exit( )
call speaks directly to the JVM,
causing it to die immediately. Since a Java program running from Ant
is running in Ant’s JVM, any calls to
System.exit( )
will kill Ant’s
JVM. This is bad. Lucky for you, the java
task has
an attribute called fork
.
<java classname="org.oreilly.SpecialTool" fork="yes"/>
The fork
attribute on the java
task gives you the ability to avoid this problem. The attribute tells
the java
task to run the class in a separate JVM.
Being in a separate JVM means the program’s
System.exit( )
call can’t kill
Ant’s JVM. While this keeps your build from breaking
unexpectedly, the problem isn’t completely solved.
The second JVM is still dying and the Java program is dying without
warning. It’s still in your best interest to try to
solve the root problem and get rid of the System.exit( )
call, if possible.
Get Ant: The Definitive Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.