控制台输出捕获的可用解决方案 - java [英] A usable solution for console output capturing - java

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

问题描述

我正在做一个程序,将有能力运行java编译器和jvm从里面(不要问我为什么我重新发明的轮,如果你的回复不帮助,保存,我已经相当沮丧的花费几个小时的解决方案,不工作!)。到目前为止,我已经管理它跟踪每当我输入的东西在我的textField以java开头,所以它会实际上包装文本,并给它一个像这样的运行:

I am making a program that will have the ability to run the java compiler and jvm right from within it (Don't ask me why I am reinventing the wheel, if your reply does not help, save it, I am already quite frustrated spending hours on solutions that do not work!). So far I have managed it to track whenever I input something in my textField that starts with java so that it will actually wrap up the text and give it a run like so:

    if(String.valueOf(object).startsWith("java")){
        try{
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec(String.valueOf(object));
        }
        catch(Exception e){gsc.mainWindow.printf("error");}


b $ b

考虑 gsc.mainWindow.printf(...); 我输出到JFrame中的JTextArea。

Consider gsc.mainWindow.printf(...); my output to a JTextArea within a JFrame.

我现在管理的是运行命令,但任何失败,我将能够直接打印到我的输出。我知道这已经回答了很多次,阅读约10种方式来做到这一点,但没有一个工作或可以理解的,我可以运行它。我需要的代码是足够简单,因为这将必须输出什么进程将编写在默认系统的控制台(cmd,终端),然后停止(我认为这可以是一个方法调用alltogether)。我对这种东西很不好,即使一个多线程解决方案可以满足我的需要,没有太专业真的,我只需要它工作。任何你需要的信息,请离开!
提前感谢! :)

What I have managed now is to run the commands, but anything fails I shall be able to print it directly to my output. I know this has been answered a ton of times before, read about 10 ways to do this, but none of them worked or was understandable to the point that I could run it. I need the code to be simple enough as this will have to be outputting what the proccess will be writing in the default system's Console (cmd,terminal) and then stop (I thought that this can be a method call alltogether). I am quite bad with this kind of stuff, even a multithread solution could fit my needs, nothing too professional really, I just need it to work. Any information you need, ask away! Thanks in advance! :)

推荐答案

我不知道你是否想读这个,但是你知道,在Java世界里,应该总是在实现自己之前寻找解决方案。解决常见问题的解决方案大多数时候来自 Apache Commons 或其他Apache项目。说你的解决方案以外的一切都不工作,或者太复杂,只会花费你的时间和金钱(和你的工作,最终)。

I don't know you if you want to read this, but you know, in the Java world, you should always look for a solution before implementing your own. And the solution for common problems, most of the time, comes from Apache Commons or other Apache projects. Saying that everything but your solution doesn't work or is too complicated to you will only cost you time and money (and your job, eventually).

Apache Commons Exec 是你需要更快,更容易地解决你的问题。

Apache Commons Exec is what you need to solve your problem faster and easier.

----编辑----

---- Edit ----

这里是一些如何捕获子进程输出的代码。只有一个类, PumpStreamHandler

Here is some code of how to capture the output of the child process. There's a class just for it, the PumpStreamHandler:

DefaultExecutor exec = new DefaultExecutor();
PumpStreamHandler streamHandler = new PumpStreamHandler();
exec.setStreamHandler(streamHandler);

CommandLine commandline = CommandLine.parse(command);  //where command is your command line
exec.execute(commandline);

----编辑2 ----

---- Edit 2 ----

这是使用 OutputStream 捕获消息的复制粘贴解决方案:

Here is the copy-paste solution you want to capture the message using an OutputStream:

public abstract class LogOutputStream extends OutputStream {

protected static final String LINE_SEPERATOR = System.getProperty("line.separator");
public static final int DEFAULT_BUFFER_LENGTH = 2048;

protected boolean hasBeenClosed = false;
protected byte[] buf;
protected int count;
private int bufLength;

public LogOutputStream() {
    bufLength = DEFAULT_BUFFER_LENGTH;
    buf = new byte[DEFAULT_BUFFER_LENGTH];
    count = 0;
}

public void close() {
    flush();
    hasBeenClosed = true;
}

public void write(final int b) throws IOException {
    if (hasBeenClosed) {
        throw new IOException("The stream has been closed.");
    }
    if (b == 0) {
        return;
    }
    if (count == bufLength) {
        final int newBufLength = bufLength + DEFAULT_BUFFER_LENGTH;
        final byte[] newBuf = new byte[newBufLength];

        System.arraycopy(buf, 0, newBuf, 0, bufLength);

        buf = newBuf;
        bufLength = newBufLength;
    }
    buf[count] = (byte) b;
    count++;
}

public void flush() {
    if (count == 0) {
        return;
    }
    if (count == LINE_SEPERATOR.length()) {
        if (((char) buf[0]) == LINE_SEPERATOR.charAt(0)
                && ((count == 1) ||
                ((count == 2) && ((char) buf[1]) == LINE_SEPERATOR.charAt(1)))) {
            reset();
            return;
        }
    }
    final byte[] theBytes = new byte[count];
    System.arraycopy(buf, 0, theBytes, 0, count);
    log(new String(theBytes));
    reset();
}


private void reset() {
    count = 0;
}

public abstract void log(String message);
}

然后只需创建一个子类,实现 public void log(String message)与更新UI的代码,完成。

Then just create a subclass of it, implement the public void log(String message) with the code that updates the UI, and it's done.

这篇关于控制台输出捕获的可用解决方案 - java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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