开发者

Running a continuous batch process in Java

开发者 https://www.devze.com 2023-04-11 16:47 出处:网络
I have a batch file which performs the operation of listening to the microphone and converting it to text (i am using pocket sphinx).

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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消