ant java任务:使用spawn = true重定向输出

安东(Anton K):

问候,

这是一个相当明确的问题。

我必须使用启动一个Java作业<java>,该作业将与ant并行运行,因此如果该作业的寿命超过了ant进程,那是可以的spawn="true"

我必须在指定文件中查看作业输出。这是通过完全实现output="job.out"spawn="false",但我还挺有走出运气spawn"=true"

因此,是否有任何中等程度的恶意破解,或者我真的必须javaexec下面类似代码包装通话吗?

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

谢谢,安东

安东(Anton K):
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);
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

的Java的ProcessBuilder:如何抑制输出而不是重定向它的

如何查看Ant在Java任务中使用的完整命令?

如何配置JUnit Ant任务以仅在失败时产生输出?

使用“屏幕”命令输出重定向

Ant Java任务:如何在不进行shell重定向的情况下将输出获取到控制台并始终记录文件生成输出

使用Ant停止并行Java任务

如何使用Java执行ant并捕获输出?

在Java 5/6中重定向ProcessBuilder的输出?

如何使用python将“打印”输出重定向到文件?

groovyc ant任务失败,并显示fork =“ true”

重定向标准输出和标准输入-Java

无法使用CreateProcess重定向标准输出

重定向从ant调用的Java任务中的日志?

输出未重定向到Windows任务内的文件

如何灵活地重定向任务中的输出

使用其他JRE在ant中运行Java任务

ant exec任务输出包括args吗?

使用相同的输入和输出文件使用ant xslt任务

Gradle:获取Ant任务输出

使用Java实时重定向控制台输出流

使用'-<<(...)'进行命令输出重定向

如何从/ usr中的jdk重定向ant使用Java,而不是/ opt中的jre重定向ant?

如何动态使用输出重定向?

使用文件属性信息重定向输出

在任务假脱机程序中重定向进程输出

Java运行时重定向C程序输出

使用 PHP 的 exec() 函数重定向 shell 命令的输出

如何从 Ant 的 <java> 任务输出中消除前导 [java] 标签?

无法覆盖使用输出重定向退出文件>