如何解决ant java任务:使用spawn = true重定向输出
| 问候, 这是一个相当明确的问题。 我必须使用<java>
启动一个Java作业,该作业将与ant并行运行,并且如果该作业的寿命超过了ant进程,则可以,因此为spawn=\"true\"
。
我必须在指定文件中查看作业输出。通过output=\"job.out\"
可以实现spawn=\"false\"
,这是完全可以实现的,但是我有点having4ѭ。
因此,有没有一点肮脏的技巧,或者我真的必须像下面这样用6来包装5的电话吗?
CMD /C my-java-command-and-hardcoded-classpath-goes-here > job.out
谢谢,
安东
解决方法
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,wrappedArgs.length);
Class.forName(mainClass).getMethod(\"main\",String[].class).invoke(null,(Object) wrappedArgs);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。