Reputation: 789
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?
Upvotes: 3
Views: 7447
Reputation: 597412
public static void main
(not Object
as return type)ObjectOutputStream
(all necessary examples are in the javadoc)exec()
, get the output with process.getOutputStream()
ObjectInputStream
based on the already retreived OutputStream
(check this)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.
Upvotes: 2
Reputation: 23980
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.
Upvotes: 0
Reputation: 104196
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.
Upvotes: 0
Reputation: 67820
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.
Upvotes: 0
Reputation: 83330
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.
Upvotes: 1