Ant java task: redirecting output with spawn = true

Hello,

here is a pretty clear question.

I need to run a java task with <java> , which should be run in parallel with ant, and this is normal if work terminates the ant process, therefore spawn="true" .

I need to see the job exit in the designated file. This is quite achievable with output="job.out" for spawn="false" , but I was not lucky that spawn"=true" .

So, is there some kind of dirty hack hack, or do I really need to wrap a java call with exec , as shown below?

 CMD /C my-java-command-and-hardcoded-classpath-goes-here > job.out 

Thanks Anton

+4
source share
1 answer
 import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; import java.lang.reflect.InvocationTargetException; import java.util.Arrays; public class StreamRedirector { public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { System.out.println(Arrays.toString(args)); // parse the arguments if (args.length != 2) { throw new IllegalArgumentException( "Usage:" + "\targ0 = wrapped main FQN;\n" + "\targ1 = dest output file name;\n" + "\tother args are passed to wrapped main;" ); } String mainClass = args[0]; String destinationFile = args[1]; // redirect the streams PrintStream outErr = new PrintStream(new FileOutputStream(destinationFile)); System.setErr(outErr); System.setOut(outErr); // delegate to the other main String[] wrappedArgs = new String[args.length - 2]; System.arraycopy(args, 2, wrappedArgs, 0, wrappedArgs.length); Class.forName(mainClass).getMethod("main", String[].class).invoke(null, (Object) wrappedArgs); } } 
+2
source

All Articles