使用Process动态运行java代码

使用Process动态运行java代码

本文介绍了使用Process动态运行java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类,它通过调用它的来动态编译,加载到CustomClassLoader中,并执行内存中 java源代码(即:没有类文件)java源代码。 main 方法。

I have created a class that dynamically compiles, loads in a CustomClassLoader, and executes an in-memory java source (i.e.: without class files) java source by invoking it's main method.

我需要捕获 StdOut StdIn StdErr ,虽然在我当前的代码中不可能这样做。 (编译器API +类加载器+反射

I need to capture the StdOut, StdIn, and StdErr, although it's not possible doing so in my current code. (Compiler API + Classloader + Reflection)

我的要求可能与在中询问 - 并按照接受的答案 - 使用。如果我在文件系统中有物理文件可用,这会更容易,但在这种情况下我没有。

My requirements might be the same as asked in this question - and as suggested by the accepted answer - use java.lang.Process. This is easier if I had physical files available in the file system, but I have not in this case.

我打算删除 Classloader + Reflection 策略并改用建议;虽然,我不熟悉实际使用Process类重定向 Std *

I am planning to remove the Classloader + Reflection strategy and use the suggestion instead; although, I'm not familiar in actually redirecting the Std* using the Process class.

如何在Java 7中执行此操作? (非常感谢网页摘要)或者更重要的是,有更好的方法吗?

How can I do this in Java 7? (Snippets are highly appreciated) Or more importantly, is there a better approach?

推荐答案

Java允许您提供自己的 PrintStream 覆盖 stdout stderr 以及 InputStream for stdin

Java allows you to supply your own PrintStream to override stdout and stderr and a InputStream for stdin.

就我个人而言,我不喜欢简单地扔掉原始流,因为我倾向于只想重定向或解析它,而不是停止它(虽然你也可以这样做)。

Personally, I don't like simply throwing away the original stream, cause I tend to only want to redirect or parse it, not stop it (although you could do that as well).

这是一个简单的例子想法......

Here is a simple example of the idea...

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

public class RedirectStdOut {

    public static void main(String[] args) {

        Consumer stdConsumer = new Consumer() {

            @Override
            public void processLine(StreamCapturer capturer, String text) {
            }

            @Override
            public void processCharacter(StreamCapturer capturer, char character) {
                capturer.getParent().print(character);
            }
        };

        StreamCapturer stdout = new StreamCapturer(stdConsumer, System.out);
        StreamCapturer stderr = new StreamCapturer(stdConsumer, System.err);

        System.setOut(new PrintStream(stdout));
        System.setErr(new PrintStream(stderr));

        System.out.println("This is a test");
        System.err.println("This is an err");

    }

    public static interface Consumer {

        public void processLine(StreamCapturer capturer, String text);

        public void processCharacter(StreamCapturer capturer, char character);
    }

    public static class StreamCapturer extends OutputStream {

        private StringBuilder buffer;
        private Consumer consumer;
        private PrintStream parent;
        private boolean echo = false;

        public StreamCapturer(Consumer consumer, PrintStream parent) {
            buffer = new StringBuilder(128);
            this.parent = parent;
            this.consumer = consumer;
        }

        public PrintStream getParent() {
            return parent;
        }

        public boolean shouldEcho() {
            return echo;
        }

        public void setEcho(boolean echo) {
            this.echo = echo;
        }

        @Override
        public void write(int b) throws IOException {
            char c = (char) b;
            String value = Character.toString(c);
            buffer.append(value);
            if (value.equals("\n")) {
                consumer.processLine(this, value);
                buffer.delete(0, buffer.length());
            }
            consumer.processCharacter(this, c);
            if (shouldEcho()) {
                parent.print(c);
            }
        }
    }

}

现在 StreamCapturer 能够在需要时回显输出,我已将其关闭以演示使用 Consumer 。我通常会使用 Consumer 来处理流中的内容,根据您的需要,您可以等待整行或处理各个字符...

Now the StreamCapturer has the ability to echo the output if you want, I've turned it off to demonstrate the use of the Consumer. I would normally use the Consumer to process what is coming through the stream, based on your needs, you can wait for the complete line or process the individual characters...

这篇关于使用Process动态运行java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:23