开发者

Get output from a process

开发者 https://www.devze.com 2022-12-16 00:08 出处:网络
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?

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?


  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.


You could do System.setOut(new PrintStream(p.getOutputStream())) if you'd like to have the process print its results directly to standard output. Of course, this will override the old standard output. But you could also do other things with the process's output stream, like have a thread that reads from it.

A problem with your code is that the main function of a class must be of type void, and will return nothing. You will not be able to pass Java objects between processes, as they are running in different JVMs. If you must do this you could serialize the object to disk, but I imagine you don't even need to run this in a separate process.


mediaProp is a local variable in your main() method. It's not accessible from the outside.

You'll have to redesign your mediaProperty class a bit.


First, you should use:

"java -cp . mediaProperty " + filename

for calling the java process. The "-cp ." defines the classpath and I have made the assumption that the java file is compiled and the generated class file is at the same path as the executing process.

Then, you need to print the result at the standard output and not just return it. Finally, read this article for reading the output.

  • Tip 1: Rename the class to MediaProperty
  • Tip 2: Why you don't call the MediaProperty class directly from your code? Is it necessary to start a new process?


There are a few gotcha's.

In exec you assume that java is on the path, and the filename should be fully qualified or you should know that the current working dir of the java process is OK.

main() should return void (nothing). If you want to pass the results out of your program use something like:

for (Object o : mediaProp) {
   System.out.println(o);
}

and parse it again on the input stream (the calling software).

Better yet, include the MediaProperty class in the java path and call main(...) directly in stead of calling a separate java process.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号