Get output from a process
Asked Answered
C

5

3

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?

Clot answered 5/1, 2010 at 16:27 Comment(3)
isn't it public static void main(...) in stead of Object?Naoise
Though it doesn't really matter class names usually start with a capital; mediaProperty -> MediaProperty.Naoise
If I don't return something, how do I get my output?Clot
O
2
  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.

Othilia answered 6/1, 2010 at 7:51 Comment(4)
@Bozho: What is the class1 and class2 in your "Convert a Java OutputStream to an InputStream" link?Clot
class1 is the process instance. Class2 you don't need - you you will create ObjectInputStream out of the ByteArrayInputStreamOthilia
I'm sorry, I don't get it. I know I need to link "out" of ObjectInputStream to process.getOutputStream. There's no such method "putDataOnOutputStream" right?Clot
You can ask another question about thatOthilia
S
1

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.

Scalpel answered 5/1, 2010 at 16:31 Comment(0)
O
0

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.

Omsk answered 5/1, 2010 at 16:33 Comment(0)
B
0

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?
Bergamot answered 5/1, 2010 at 16:39 Comment(1)
Yes, I need a process, long story refer to here:#1998085Clot
N
0

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.

Naoise answered 5/1, 2010 at 16:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.