IDE崩溃后读取控制台应用程序的标准输出

时间:2019-01-28 15:26:03

标签: windows eclipse stdout

有时Eclipse死亡,并且使Java控制台程序运行。我想在Eclipse死后访问控制台输出。

我该怎么做?

我可以在任务管理器中看到控制台程序,因此它仍在运行。

2 个答案:

答案 0 :(得分:0)

最简单的方法可能是save the output to a file

这最适合大型日志,但也适用于您的情况。

答案 1 :(得分:0)

看起来我能够通过使用简单的代码编写自定义Agent.jar来做到这一点

import java.io.File;
import java.io.PrintStream;
import java.lang.instrument.Instrumentation;

public class Agent {
    public static void premain(String args, Instrumentation instrumentation) {

    }

    public static void agentmain(String agentArgs) {
        try {
            PrintStream psout =new PrintStream(new File("C:\\LOG\\out.txt"));
            PrintStream pserr =new PrintStream(new File("C:\\LOG\\err.txt"));
            System.setOut(psout);
            System.setErr(pserr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

包含预先创建的文件。 我用MANIFEST.MF中的Agent-Class字符串将其制成罐子 然后像“ p”一样简单地“注入”到运行中的虚拟机

public class TestAttachJVM {
    public static void main(String s[]) throws Exception {
        VirtualMachine vm = VirtualMachine.attach("<PID>");
        try { 
          // load agent into target VM
          String agent = "c:\\DIR\\agent.jar";
          vm.loadAgent(agent);
        } finally {    
          // detach
          vm.detach();
        }
    }
}

然后,我完成了此简单程序的执行,out.txt和err.txt被锁定。 不幸的是,我看不到立即输出,因为我尝试挂接的程序每1-4小时就会产生任何输出。我待会再检查。

相关问题