Question

Ant Java task provides fork parameter, which, by definition "if enabled triggers the class execution in another VM". As we are dealing with a large amount of data, setting this parameter saved us from running out of Java heap space.
We want to be able to do the same through a Java class. What would be the best way to achieve the functionality as provided by fork?

Was it helpful?

Solution

Execute another java process. By using ProcessBuilder class, for example.

http://java.sun.com/javase/6/docs/api/java/lang/ProcessBuilder.html

You can run as many worker processes as you wish. Make them having a separate main class, doing their tasks from that main class, and quiting when their task is completed.

You'll have to figure out their classpath, and the location of java binary on the system, but that's doable.

I think you can even be notified when they complete via Process.waitFor().

OTHER TIPS

If you look at the ant source code, when fork is true, then it just wraps an Execute task and eventually, the code that gets called is

Runtime.getRuntime().exec(cmd, env);

Downloading and having a look at the source code for org.apache.tools.ant.taskdefs.Java and org.apache.tools.ant.taskdefs.Execute will give you some great pointers in finding the location of the executable to run in a platform independent way, etc.

I think you can directly use the Ant API...Ant can be directly used in a Java class. The Javadoc is available in their binary distribution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top