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);
}
}
...全文
276 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
meyew 2005-03-31
  • 打赏
  • 举报
回复
再顶一下
meyew 2005-03-23
  • 打赏
  • 举报
回复
我是加在open()方法中的p = Runtime.getRuntime().exec(startcmd);之后,一下就出来Matlab界面了,若是不加,则很慢才出来。

takecare(大厅)兄,能否抽空帮看一下,这个程序也是从网上找来的,我对线程之类的不大懂。
takecare 2005-03-23
  • 打赏
  • 举报
回复
没有细看你的代码,应该在你从runtime获取process后,就让那些gobbler启动起来。
但是我觉得里头有个流应该是你需要的,所以需要将那个gobbler将截获的输出重定向到你要的那个流里。
meyew 2005-03-22
  • 打赏
  • 举报
回复
参照楼上给出的贴子,加了以下几个语句。


StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");

// kick off stderr
errorGobbler.start();

StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(), "STDOUT");
// kick off stdout
outGobbler.start();


运行几乎很快,但光标还是停在matlab上。
不知加在何处,请takecare(大厅)指点。
takecare 2005-03-11
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/3165/3165480.xml?temp=.8489801
takecare 2005-03-11
  • 打赏
  • 举报
回复
呵呵。老问题了,runtime的io要自己控制的。搞个线程去取空那些输出,比如stdou和stderr
BasaraTracy 2005-03-11
  • 打赏
  • 举报
回复
up
飞行的兔子 2005-03-09
  • 打赏
  • 举报
回复
你在读入数据后,调用flush(0函数将缓冲区刷新一下看看,因为你用了缓冲了.
其它方面就不太懂!
meyew 2005-03-09
  • 打赏
  • 举报
回复
哪位高手帮忙解答一下,非常急!
meyew 2005-03-04
  • 打赏
  • 举报
回复
哪位高手帮忙解答一下,非常急!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧