java的process问题
meyew 2005-03-04 09:15:38 下面程序是java对matlab的调用,用来求解一线性程组,java向matlab输入参数,并输出所计算的解。运行中,matlab启动后程序即不再往下运行。不知什么原因,请高手指点。好象receive()方法中的in.ready()有些问题。
main类:
import MatlabRuntimeInterface.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
Engine engine = new MatlabRuntimeInterface.Engine();
try {
engine.open("matlab -nosplash -nojvm");
System.out.println(engine.getOutputString(500));
engine.evalString("A = gallery('lehmer',10);"); // 矩阵 A
engine.evalString("f = ones(10,1);"); // 向量 f
engine.evalString("pcg(A,f,1e-5)"); // 解 x
System.out.println(engine.getOutputString(500));
engine.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Engine类:
package MatlabRuntimeInterface;
import java.lang.Runtime;
import java.io.*;
public class Engine {
public Engine() {
outputBuffer = new char[DEFAULT_BUFFERSIZE];
}
private boolean isOpen = false;
private Process p;
private BufferedReader in;
private BufferedWriter out;
private BufferedReader err;
static final int DEFAULT_BUFFERSIZE = 65536;
private char[] outputBuffer;
private static final int DEFAULT_SKIP = 65536;
private int totalCharsRead;
public void open() throws IOException {
open("matlab");
}
public void open(String startcmd) throws IOException {
if (isOpen) {
System.err.println("Matlab session already open.");
return;
}
try {
synchronized(this) {
p = Runtime.getRuntime().exec(startcmd);
out = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
isOpen = true;
}
receive();
}
catch(IOException e) {
System.err.println("Matlab could not be opened.");
throw(e);
}
}
public void close() throws InterruptedException, IOException {
send("exit");
try {
synchronized(this) {
p.waitFor();
out.close();
in.close();
isOpen = false;
}
}
catch(InterruptedException e) {
throw(e);
}
catch(IOException e) {
System.err.println("Error while closing input/output streams.");
throw(e);
}
}
private void send(String str) throws IOException {
try {
str+="\n";
synchronized(this) {
out.write(str, 0, str.length());
out.flush();
}
}
catch(IOException e) {
System.err.println("IOException occured while sending data to the" +" Matlab process.");
throw(e);
}
}
private void skip() throws IOException {
char[] skipBuffer = new char[DEFAULT_SKIP];
while(in.ready()) {
in.read(skipBuffer,0,DEFAULT_SKIP);
}
System.err.println("Warning: Some output information was "+ "dropped, since the available number of "+
"bytes exceeded the output buffer "+
"size.");
}
private void receive() throws IOException {
int charsRead = 0;
int numberToRead;
totalCharsRead = 0;
try {
synchronized(this) {
while (totalCharsRead == 0) {
while (in.ready()) {/////好象是这儿出现问题???
if ((numberToRead = DEFAULT_BUFFERSIZE-totalCharsRead) > 0) {
charsRead = in.read(outputBuffer, totalCharsRead, numberToRead);
if (charsRead > 0) {
totalCharsRead += charsRead;
}
}
else {
skip(); return;
}
}
}
}
}
catch(IOException e) {
System.err.println("IOException occured while receiving data from"+" the Matlab process.");
throw(e);
}
}
public void evalString(String str) throws IOException {
send(str);
receive();
}
public String getOutputString (int numberOfChars) {
if (totalCharsRead < numberOfChars) numberOfChars = totalCharsRead;
char[] result = new char[numberOfChars];
System.arraycopy(outputBuffer, 0, result, 0, numberOfChars);
return new String(result);
}
}