在 Java 中运行连续批处理
我有一个批处理文件,它执行监听麦克风并将其转换为文本的操作(我正在使用袖珍狮身人面像)。
我用来运行批处理文件的命令是pocketsphinx_continuous.exe -dict
我试图将其制作成一个独立的 Java 应用程序。我想通过 Java 运行这个批处理文件,因此我使用了 Runtime.getRuntime().exec("cmd /c start pocketsphinx_continuous.exe ...")
以及所有参数。但奇怪的是,它在单独的命令提示符中启动批处理过程,但立即退出。我尝试使用 process.waitfor(),但它只是开始执行批处理,然后终止。我还没有调用process.destroy
,所以我无法弄清楚它为什么退出批处理过程。
另一个问题是,由于批处理文件是连续运行的,在转录每个口语句子后,我希望在我的 Java 应用程序中获得输出。我知道我可以将批处理过程重定向到一个文件,然后读取该文件,只是想知道是否有更直接的过程。您能帮我找出我在哪里犯了错误吗?
I have a batch file which performs the operation of listening to the microphone and converting it to text (i am using pocket sphinx).
The command I am using to run the batch file is pocketsphinx_continuous.exe -dict <dict name> -lm <language model> -hmm <acoustic model location>
. The batch file starts off and keeps listening to the microphone. Whenever we finish speaking a sentence it converts it into text on the command prompt. Since this continuously running we terminate this task by Ctrl-C.
I was trying to make this into a standalone Java application. I wanted to run this batch file through Java, so i used Runtime.getRuntime().exec("cmd /c start pocketsphinx_continuous.exe ...")
with all the parameters. However strangely, it starts the batch process in a separate command prompt but immediately exits. I tried to use process.waitfor()
, however it simply starts executing the batch process and then terminates. I havent called process.destroy
, so i am unable to figure out why it exits the batch process.
The other question is since the batch file is running continuously, after every spoken sentence has been transcribed , I wish to get the output in my Java application. I know i can redirect the batch process to a file and then read the file, was just wondering if there is a more direct process. Could you please help me figure out where I am making a mistake.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用
Process .getInputStream()
和Process.getErrorStream()
查看它打印出哪些消息。如果它立即退出,您可能需要获取退出代码 (
Process.waitFor()
) 并从错误流中检查错误日志。另外,如果您可以发布一些代码,我们也许可以提供帮助。一般来说,这些问题是由于路径或命令字符串配置不正确造成的。
可能的解决方法是使用
ProcessBuilder< /code>
来自 Java 1.5:
正如 Joachim Sauer 在评论中提到的,这个 Javaworld 文章 解释了许多与处理API,所以通读一下。我还没有机会体验 JDK7 中对 Process API 所做的改进,从表面上看,Oracle 正在针对 JDK8 再次改进它(JEP 102)。
You should use
Process.getInputStream()
andProcess.getErrorStream()
to find out what messages it prints out.If it is exiting instantly, you might need to get the exit code (
Process.waitFor()
) and check the error logs from the error stream.Also, if you can post some of your code we might be able to help. In general, these problems are due to incorrectly configured paths or command strings.
A possible fix would be to use the
ProcessBuilder
from Java 1.5 thusly:As Joachim Sauer mentioned in the comments, this Javaworld article explains a lot of the gotchas associated with the Process API, so have a read through. I haven't had a chance to play with the improvements made in JDK7 to the Process API, and by the looks of things Oracle are improving it again for JDK8 (JEP 102).
您还可以使用Timer和TimerTask在后台安排批量扫描过程。您可以使用它来指定无限运行的任务。
You can also use Timer and TimerTask to schedule your batch scan process in background. You can use this to specify infinite running task.