在Java中捕获特定线程的控制台输出 [英] Capture Console Output of a Specific Thread in Java

查看:872
本文介绍了在Java中捕获特定线程的控制台输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想要一个方法,在给定一个Class对象的情况下,调用main方法,也就是说,public static void main,对该类(如果存在)捕获该主方法的控制台输出。执行调用的类是非守护线程。



我已经有部分代码,但我不知道如何捕获控制台输出,顶部,如何只捕获它*这个特定的线程。这是我到目前为止:

  public class Output class Thread {
private Class testClass;

public Output(Class clazz){
this.testClass = clazz;
}

private方法getMainMethod(Class clazz){
Method [] methods = clazz.getMethods();
for(Method method:methods){
if(isMainMethod(method)){
return method;
}
}

return null;
}

private Boolean isMainMethod(Method method){
return(method.getName()。equals(main)&&
Modifier.isStatic (method.getModifiers())&&
method.getReturnType()。equals(Void.class));
}

public void run(){
方法mainMethod = null;

if((mainMethod = getMainMethod(this.testClass))== null){
//如果没有静态void main方法,抛出异常
throw new YouFuckedItUpException() ;
}

mainMethod.invoke(this.testClass,new String [0]);

return heresWhereImStuckIWantToCaptureTheConsoleOutputAndReturnIt();
}
}



所有我需要的是一些代码,到如何捕获被调用的方法的System.out和System.err输出的答案。



提前感谢!



/ strong>这不是仅用于测试,这最终会被投入生产。



EDIT 2 :这将需要线程-安全。多个主方法将被其他线程同时调用,我希望每个线程只捕获自己的具体输出。

解决方案

因为你使用 main()方法 - 你需要它在同一个进程吗?如果没有,您可以尝试将其创建为一个新进程( java.lang.Process )。



Process 类提供了捕获 StdOut StdErr 和/或 StdIn



注意因为一切都在自己的进程中运行,线程没有问题-安全。但是你仍然必须找到位置,.class文件(或至少是根)位于,以便您可以运行/创建进程与 java here.goes.your.ClassName


I realize there are similar questions on SO, but they don't quite solve my problem.

I would like a method that, given a Class object, will invoke the "main" method, that is to say, public static void main, on that Class (if it exists) and capture the console output of that main method. The class doing the invocation is a non-daemon thread.

I have part of the code already, but I'm not sure how to capture the console output, and on top of that, how to only capture it for *this specific thread. Here's what I have so far:

public class Output extends Thread {
    private Class testClass;

    public Output(Class clazz) {
        this.testClass = clazz;
    }

    private Method getMainMethod(Class clazz) {
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            if (isMainMethod(method)) { 
                return method;
            }
        }

        return null;
    }

    private Boolean isMainMethod(Method method) {
        return (method.getName().equals("main") &&
                Modifier.isStatic(method.getModifiers()) &&
                method.getReturnType().equals(Void.class));
    }

    public void run() {
        Method mainMethod = null;

        if ((mainMethod = getMainMethod(this.testClass)) == null) {
            //if there's no static void main method, throw exception
            throw new YouFuckedItUpException();
        }

        mainMethod.invoke(this.testClass, new String[0]);

        return heresWhereImStuckIWantToCaptureTheConsoleOutputAndReturnIt();
    }
}

All I need is some code to, or a link to an answer on how to, capture the System.out and System.err output of the method being invoked. Any help someone can give would be much appreciated.

Thanks in advance!

EDIT: This is NOT for testing only, this will eventually be something put into production.

EDIT 2: This will need to be thread-safe. Multiple main methods will be invoked by other threads possibly at the same time, and I want each thread to only capture its own specific output.

解决方案

Since you use the main() method - do you need it to be in the same process? If not, you may try to create it as a new Process (java.lang.Process).

The Process class provides the necessary methods to capture StdOut, StdErr and/or StdIn.

Note Since everything runs in it's own process, there should be no issues with thread-safety. However you will still have to find the location, the .class file (or at least the root) is located in, so that you can run/create the Process with java here.goes.your.ClassName.

这篇关于在Java中捕获特定线程的控制台输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆