Reputation: 3284
I'm using java.lang.ProcessBuilder
in Matlab to build and start a process. This process gets passed to a Java class that reads data from the process.
In Matlab I have this:
temp_process_builder = java.lang.ProcessBuilder(cmd_array);
%Starting the process
%--------------------------------------------------
obj.j_process = temp_process_builder.start();
obj.j_error_stream = obj.j_process.getErrorStream;
obj.j_input_stream = obj.j_process.getInputStream;
obj.j_output_stream = obj.j_process.getOutputStream;
On Windows, the classes are:
java.lang.ProcessImpl
for the processjava.io.FileInputStream
for errorjava.io.BufferedInputStream
for the inputjava.io.BufferedOutputStream
for the output
The Java constructor I'm calling is:
public NEURON_reader(BufferedInputStream pin, FileInputStream perr, Process p) {
this.p = p;
this.perr = perr;
this.pin = pin;
}
Later calls are made to read()
and available()
methods of perr
and pin
.
I recently tried running the code using a Mac machine, but now the classes are:
java.lang.UNIXProcess
for the processjava.lang.UNIXProcess$ProcessPipeInputStream
for the error and inputjava.lang.UNIXProcess$ProcessPipeOutputStream
for the outputPassing these variables to my Java code results in an error due to the mismatch in data types => "No constructor 'NEURON_reader' with matching signature found."
My question is, is it possible to easily convert the unix process values to what I'm seeing on Windows. Alternatively, is there some easy way of writing a generic wrapper that behind the scenes uses one or the other? Put simply, what's the best way of making my old code run on Macs?
On a final note, I only use the input and error streams in Java, whereas I write to the output stream in Matlab. Not sure if that helps ...
Upvotes: 0
Views: 431
Reputation: 201447
Program to the interfaces instead of the concrete types. Instead of java.lang.ProcessImpl
use the abstract class ("interface") java.lang.Process
. And instead of BufferedInputStream
and FileInputStream
use the InputStream
interface.
public NEURON_reader(InputStream pin, InputStream perr, Process p) {
Note that in Java while the implementations returned on various platforms may differ, the documented public interfaces remain the same.
Upvotes: 1