由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 请教一个问题,thanks!
相关主题
Re: How can I call another program from Java?[请教]调用外部命令编码的问题
问个io的问题string pool vs class constant pool问题,在线等
问一个blocking IO的程序怎么样让jar在64位JRE下运行啊?
怎么从键盘输入整数或float?Re: The real Risk
read from multiple inputstreams at the same time?JVM
请问同时执行几个bat文件的问题JCreator如何调试程序?
Java program running on PDA多少个thread 就算不正常?
Java runtime array memory layout?Re: 如何从键盘输入获得一个float值?谢谢!
相关话题的讨论汇总
话题: 程序话题: string话题: prg话题: 外部话题: printf
进入Java版参与讨论
1 (共1页)
M*****n
发帖数: 2301
1
想在java里面调用一个外部可执行文件,这个外部程序本来是打印一个浮点数
到屏幕上,就是printf("%f", ans); 这样的,
我想在java程序里面capture这个值,我得code是这样的
Runtime rt = java.lang.Runtime.getRuntime();
process = rt.exec(command);

datastream = new DataInputStream(process.getInputStream());
result = datastream.readFloat();
可是总是死在 result = datastream.readFloat();这一行上,
请教大侠怎么解决?多谢!
另外,怎知道外部程序的输出确实读进来了呢? 一般用什么debug啊
我用jcreator,编辑还行,debug感觉比较弱。
e***g
发帖数: 158
2
printf("..\n"

【在 M*****n 的大作中提到】
: 想在java里面调用一个外部可执行文件,这个外部程序本来是打印一个浮点数
: 到屏幕上,就是printf("%f", ans); 这样的,
: 我想在java程序里面capture这个值,我得code是这样的
: Runtime rt = java.lang.Runtime.getRuntime();
: process = rt.exec(command);
:
: datastream = new DataInputStream(process.getInputStream());
: result = datastream.readFloat();
: 可是总是死在 result = datastream.readFloat();这一行上,
: 请教大侠怎么解决?多谢!

st
发帖数: 1685
3
below should work. I think the output is string instead of float. I seem
to have said that before.
String temp=datastream.readLine();
float result=Float.valueOf(temp);

【在 e***g 的大作中提到】
: printf("..\n"
M*****n
发帖数: 2301
4
好像还是不行。debug执行到String temp = cs2result.readLine();
的时候就停了
是不是因为读output的时候那个process还没执行完毕的原因?
那个process是个大计算,怎么能让java等那个process terminate
以后在去读output呢?
process.waitFor(); 好像也不行啊
多谢!

【在 M*****n 的大作中提到】
: 想在java里面调用一个外部可执行文件,这个外部程序本来是打印一个浮点数
: 到屏幕上,就是printf("%f", ans); 这样的,
: 我想在java程序里面capture这个值,我得code是这样的
: Runtime rt = java.lang.Runtime.getRuntime();
: process = rt.exec(command);
:
: datastream = new DataInputStream(process.getInputStream());
: result = datastream.readFloat();
: 可是总是死在 result = datastream.readFloat();这一行上,
: 请教大侠怎么解决?多谢!

st
发帖数: 1685
5
printf("%f\n",ans);

【在 M*****n 的大作中提到】
: 好像还是不行。debug执行到String temp = cs2result.readLine();
: 的时候就停了
: 是不是因为读output的时候那个process还没执行完毕的原因?
: 那个process是个大计算,怎么能让java等那个process terminate
: 以后在去读output呢?
: process.waitFor(); 好像也不行啊
: 多谢!

v******e
发帖数: 63
6
Different JVM has different behavior on this issue. The safe way would be to
use read() in InputStream API and check return value. Also try to let the
external program sleep for a few minutes before exit.

【在 M*****n 的大作中提到】
: 想在java里面调用一个外部可执行文件,这个外部程序本来是打印一个浮点数
: 到屏幕上,就是printf("%f", ans); 这样的,
: 我想在java程序里面capture这个值,我得code是这样的
: Runtime rt = java.lang.Runtime.getRuntime();
: process = rt.exec(command);
:
: datastream = new DataInputStream(process.getInputStream());
: result = datastream.readFloat();
: 可是总是死在 result = datastream.readFloat();这一行上,
: 请教大侠怎么解决?多谢!

m******t
发帖数: 2416
7

I'm not sure I understand what you are trying to do. Are you trying to do
something after starting to exec the external program, and come back to read
the output only when it's done?

【在 M*****n 的大作中提到】
: 好像还是不行。debug执行到String temp = cs2result.readLine();
: 的时候就停了
: 是不是因为读output的时候那个process还没执行完毕的原因?
: 那个process是个大计算,怎么能让java等那个process terminate
: 以后在去读output呢?
: process.waitFor(); 好像也不行啊
: 多谢!

M*****n
发帖数: 2301
8
我把那个外部程序换成一个简单的 printf("%f\n", 3.14);
有没有事情了,stdout的输出可以被java得到,所以那个读stdout输出的
的code应该是对的。
我原来的计算程序格式是
prg < file
重定向输入一个文件。(这个程序不接受参数 prg file, 只能通过重定向)
如果我在java里面调用这个prg, 可以
直接写 command = new String("prg < file")么?
还是要用通过写process.getOutputStream()来 给prg 参数?
下面是我得code,不过好像还是不行啊:(
command = new String("prg");
Runtime rt = java.lang.Runtime.getRuntime();
process = rt.exec(command);

cs2input = new BufferedWriter(new OutputStreamWriter(process.getOutputSt
ream()));

【在 st 的大作中提到】
: printf("%f\n",ans);
M*****n
发帖数: 2301
9
对,就是java调用一个外部执行文件,然后程序里面读它的输出(那个程序
输出到stdout)

【在 m******t 的大作中提到】
:
: I'm not sure I understand what you are trying to do. Are you trying to do
: something after starting to exec the external program, and come back to read
: the output only when it's done?

t****s
发帖数: 141
10
不一定work,你还是重定向到一个文件,然后读那个文件比较管用。

【在 M*****n 的大作中提到】
: 对,就是java调用一个外部执行文件,然后程序里面读它的输出(那个程序
: 输出到stdout)

a******l
发帖数: 3
11
don't debug, just try to run to see if it works.
you might need to change your IDE.

【在 M*****n 的大作中提到】
: 对,就是java调用一个外部执行文件,然后程序里面读它的输出(那个程序
: 输出到stdout)

f********f
发帖数: 475
12
建议你用temp = cs2result.read()而不是readLine()试试。

【在 M*****n 的大作中提到】
: 我把那个外部程序换成一个简单的 printf("%f\n", 3.14);
: 有没有事情了,stdout的输出可以被java得到,所以那个读stdout输出的
: 的code应该是对的。
: 我原来的计算程序格式是
: prg < file
: 重定向输入一个文件。(这个程序不接受参数 prg file, 只能通过重定向)
: 如果我在java里面调用这个prg, 可以
: 直接写 command = new String("prg < file")么?
: 还是要用通过写process.getOutputStream()来 给prg 参数?
: 下面是我得code,不过好像还是不行啊:(

1 (共1页)
进入Java版参与讨论
相关主题
Re: 如何从键盘输入获得一个float值?谢谢!read from multiple inputstreams at the same time?
help! string format???请问同时执行几个bat文件的问题
问个很简单的问题?Java program running on PDA
BufferedWriter里的write()Java runtime array memory layout?
Re: How can I call another program from Java?[请教]调用外部命令编码的问题
问个io的问题string pool vs class constant pool问题,在线等
问一个blocking IO的程序怎么样让jar在64位JRE下运行啊?
怎么从键盘输入整数或float?Re: The real Risk
相关话题的讨论汇总
话题: 程序话题: string话题: prg话题: 外部话题: printf