Bug 47854

Summary: Ant can't run within another Java program
Product: Ant Reporter: David Byrden <bugzilla>
Component: CoreAssignee: Ant Notifications List <notifications>
Status: NEEDINFO ---    
Severity: enhancement    
Priority: P2    
Version: 1.7.1   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   

Description David Byrden 2009-09-16 14:46:02 UTC
My current shell is XMLSH, a Java program that loads Ant into its own VM. But I had to modify 2 classes to make this work. Ant 1.7.1 is an ill-behaved guest in the VM Because:

[1] It calls System.exit() which kills the entire VM. 
[2] It makes an unwarranted assumption about its ClassLoaders

Re [1] : there are calls to System.exit() in both the Launcher class and the Main class. I can extend Main to change this, but not Launcher.

In my opinion (I have Java programmer certification) no Java program should need to kill the VM unless something is wrong. 

Re [2] : on loading Ant, I see this:
"org.apache.tools.ant.Main cannot be cast to org.apache.tools.ant.launch.AntMain"

Reason: the Launcher class makes 2 ClassLoaders and assumes they will connect up in a parent-child relationship. For me, they don't. I can see no reason why Launcher should not make that connection explicitly.

Note: after changing these 2 classes, I am able to run Ant under XMLSH with a very simple wrapper script.

David
Comment 1 Martin Gainty 2009-09-25 11:12:42 UTC
i just saw this post noting 
"the Launcher class makes 2 ClassLoaders and assumes they will connect
up in a parent-child relationship. For me, they don't. I can see no reason why
Launcher should not make that connection explicitly."

i came upon this issue when another Java apache project produced classloader error
"org.apache.tools.ant.Main cannot be cast to
org.apache.tools.ant.launch.AntMain"

(attempting to pass its own CL to Ant and failed)

can we vote to accept davids mods as a workaround to this issue?

thanks
Martin
Comment 2 J.M. (Martijn) Kruithof 2009-10-01 13:56:50 UTC
Launcher only calls System.exit() in case something went wrong (non-zero exit status) Main also calls System.exit for a 0 exit status in case nothing went wrong (This may not be necessary, will investigate). Launcher is strictly meant to be used from the command line. Does it make sense to modify Launcher to solve [2] without fully solving [1], please note the System.exit calls are needed in Launcher and Main to indicate to the shell that something went wrong.
Comment 3 Stefan Bodewig 2009-10-09 01:12:23 UTC
Like Martijn said, Ant's Main has a very good reason to call System.exit, namely to have control over the process' exit code which is vital to many shell-script/batch-file scenarios.

The System.exit in Launcher will only ever be triggered if the AntMain implementation throws an exception.  Since you need to plug in your own AntMain implementation anyway, this should be easy to avoid.

For [2]: what does you modified implementation look like and would you be willing to contribute it?