在 Java 中运行连续批处理

发布于 2024-12-09 03:14:37 字数 665 浏览 0 评论 0原文

我有一个批处理文件,它执行监听麦克风并将其转换为文本的操作(我正在使用袖珍狮身人面像)。

我用来运行批处理文件的命令是pocketsphinx_continuous.exe -dict-lm <语言模型> -hmm <声学模型位置>。批处理文件开始并持续监听麦克风。每当我们说完一个句子时,它就会在命令提示符下将其转换为文本。由于此持续运行,我们通过 Ctrl-C 终止此任务。

我试图将其制作成一个独立的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩发小公举 2024-12-16 03:14:37

您应该使用 Process .getInputStream()Process.getErrorStream() 查看它打印出哪些消息。

如果它立即退出,您可能需要获取退出代码 (Process.waitFor()) 并从错误流中检查错误日志。

另外,如果您可以发布一些代码,我们也许可以提供帮助。一般来说,这些问题是由于路径或命令字符串配置不正确造成的。

可能的解决方法是使用 ProcessBuilder< /code>来自 Java 1.5:

// Note the lack of "cmd \c" - you shouldn't need to run it in a cmd window!
ProcessBuilder pb = new ProcessBuilder("pocketsphinx_continuous.exe",
                                       "dict", "1121.dic",
                                       "-lm", "1121.lm",
                                       "-hmm", "hub4/hmm");

Process p = pb.start();

// TODO for you:
// 1. Create Threads to handle the input
// 2. Store the Process instance so that you can call p.destroy() later.
// 3. If interested, have a Thread doing p.waitFor() to get the exit code.

正如 Joachim Sauer 在评论中提到的,这个 Javaworld 文章 解释了许多与处理API,所以通读一下。我还没有机会体验 JDK7 中对 Process API 所做的改进,从表面上看,Oracle 正在针对 JDK8 再次改进它(JEP 102)。

You should use Process.getInputStream() and Process.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:

// Note the lack of "cmd \c" - you shouldn't need to run it in a cmd window!
ProcessBuilder pb = new ProcessBuilder("pocketsphinx_continuous.exe",
                                       "dict", "1121.dic",
                                       "-lm", "1121.lm",
                                       "-hmm", "hub4/hmm");

Process p = pb.start();

// TODO for you:
// 1. Create Threads to handle the input
// 2. Store the Process instance so that you can call p.destroy() later.
// 3. If interested, have a Thread doing p.waitFor() to get the exit code.

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).

星星的軌跡 2024-12-16 03:14:37

您还可以使用TimerTimerTask在后台安排批量扫描过程。您可以使用它来指定无限运行的任务。

You can also use Timer and TimerTask to schedule your batch scan process in background. You can use this to specify infinite running task.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文