Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get output from a process

Tags:

java

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?

like image 224
HH. Avatar asked Apr 09 '26 15:04

HH.


1 Answers

  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.

like image 125
Bozho Avatar answered Apr 11 '26 03:04

Bozho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!