Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

java - Get output from a process

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);

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;
    }
}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...