从流程中获取输出 [英] Get output from a process

查看:63
本文介绍了从流程中获取输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题的第二部分此处

This is a second part to my question here.

我现在有一个流程,但我想知道如何从流程中获取输出?

I now have a process but I want to know how to get the output from the process?

String filename = matlab.getfileName();  
Process p = Runtime.getRuntime().exec("java -cp mediaProperty.java " + filename);

我的mediaProperty.java:

My mediaProperty.java:

public class mediaProperty {

    public static Object main(String[] args) {
        Object[] mediaProp = null;
        java.util.List lstMedia = new ArrayList();
        Media media = null;

        try {
            media = new Media();
            lstMedia.add(args);
            mediaProp = media.media(3, lstMedia);

        } catch (Exception p) {
            System.out.println("Exception: " + p.toString());
        } finally {
            MWArray.disposeArray(mediaProp);
            if (media != null) {
                media.dispose();
            }
        }
        return mediaProp;
    }
}

mediaProperty.java将返回一个Object。这里面实际上是String数组。我如何获得阵列?我调用exec()的方式是正确的吗?

The mediaProperty.java will return an Object. Inside this is actually String array. How do I get the array? And is the way I'm calling exec() correct?

推荐答案


  1. 使用 public static void main (不是 Object 作为返回类型)

  2. 使用 ObjectOutputStream (所有必要的例子都在javadoc中)

  3. 唯一与示例不同的是构造 - 构造它就像
    ObjectOutputStream oos = new ObjectOutputStream(System。 out);

  4. 在程序中调用 exec(),使用获取输出process.getOutputStream()

  5. 读入 ObjectInputStream 基于已经检索过的 OutputStream 检查这个

  6. 反序列化对象(参见ObjectInputStream的javadoc)

  1. use public static void main (not Object as return type)
  2. Serialize the object using ObjectOutputStream (all necessary examples are in the javadoc)
  3. The only thing different from the example is the construction - construct it like ObjectOutputStream oos = new ObjectOutputStream(System.out);
  4. in the program calling exec(), get the output with process.getOutputStream()
  5. Read in an ObjectInputStream based on the already retreived OutputStream (check this)
  6. Deserialize the object (see the javadoc of ObjectInputStream)

现在,这是一种奇怪的方式,但由于我不确切知道你想要实现什么,这听起来很合理。

Now, this is a weird way to do it, but as I don't know exactly what you are trying to achieve, it sounds reasonable.

这篇关于从流程中获取输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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