语音识别jar在其执行时抛出错误语法类未找到

时间:2014-07-22 10:42:38

标签: java jar cmusphinx

我通过Sphinx在JAVA中创建了一个关于语音识别的项目。我的代码执行得很完美但是当我通过“runnable jar file->将所需的库复制到子文件夹”创建它的jar,并通过命令“java -jar {jar name} .jar”通过cmd执行它时它会打开但是之后选择说话按钮或调用sphinx方法,它会发现错误edu.cmu.sphinx.jsapi.JSGFGrammar类未找到。 我没有办法解决这个问题。 我对文本代码的发言是:

package com.ongraph;
import edu.cmu.sphinx.frontend.util.Microphone;
import edu.cmu.sphinx.recognizer.Recognizer;
import edu.cmu.sphinx.result.Result;
import edu.cmu.sphinx.util.props.ConfigurationManager;

public class SpeechToTextOperation {
    ConfigurationManager cm;
    SpeechRecognizer speechRecognizer;
    Result result;
    Recognizer recognizer;
    Microphone microphone;
    private final static String STOP = "stop";
    private final static String XML_FILE = "helloworld.config.xml";

    public void voiceGet() throws InterruptedException {
        String resultString = null;
        int count_Check = 0;
        if (cm == null) {
            cm = new ConfigurationManager(getClass().getClassLoader().getResource(XML_FILE));
        }
        if (recognizer == null) {
            recognizer = (Recognizer) cm.lookup("recognizer");
            microphone = (Microphone) cm.lookup("microphone");
            microphone.clear();
        }
        recognizer.allocate();
        if (!(microphone.startRecording())) {
            System.out.println("Cannot start microphone.");
            recognizer.deallocate();
            System.exit(1);
        }
        instructions();
        //SpeechRecognizer.please_Speak.setVisible(true);
        while (true) {
            System.out
                    .println("Start speaking. Speak 'Stop' to Stop Recording.");
            if(count_Check == 0)
            {
                SpeechRecognizer.textArea.append("\n Start speaking...\n");
                count_Check++;
            }

            Result result = recognizer.recognize();
            resultString = result.getBestFinalResultNoFiller();
            if (resultString != null && !"".equals(resultString)
                    && !resultString.contains(STOP)) {
                SpeechRecognizer.textArea.append(resultString + "\n");
            } else {
                SpeechRecognizer.textArea
                        .append("'Application Stopped. Press 'Speak' again to restart'");
                recognizer.deallocate();
                microphone.stopRecording();
                break;
            }
        }
    }

    public void voiceStop() {
        microphone.clear();
        cm = null;
    }

    public void instructions() {
        // TODO Auto-generated method stub
        SpeechRecognizer.please_Speak.setVisible(true);

    }

}

cmd中的错误是:

class not found !java.lang.ClassNotFoundException: edu.cmu.sphinx.jsapi.JSGFGrammar
Exception in thread "AWT-EventQueue-0" Property Exception component:'flatLinguist' property:'grammar' - mandatory property is not set!
edu.cmu.sphinx.util.props.InternalConfigurationException
        at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:291)
        at edu.cmu.sphinx.linguist.flat.FlatLinguist.newProperties(FlatLinguist.java:246)
        at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:460)
        at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:279)
        at edu.cmu.sphinx.decoder.search.SimpleBreadthFirstSearchManager.newProperties(SimpleBreadthFirstSearchManager.java:179)
        at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:460)
        at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:279)
        at edu.cmu.sphinx.decoder.AbstractDecoder.newProperties(AbstractDecoder.java:65)
        at edu.cmu.sphinx.decoder.Decoder.newProperties(Decoder.java:37)
        at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:460)
        at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:279)
        at edu.cmu.sphinx.recognizer.Recognizer.newProperties(Recognizer.java:90)
        at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:460)
        at edu.cmu.sphinx.util.props.ConfigurationManager.lookup(ConfigurationManager.java:161)
        at com.ongraph.SpeechToTextOperation.voiceGet(SpeechToTextOperation.java:24)
        at com.ongraph.SpeechRecognizer$1.actionPerformed(SpeechRecognizer.java:50)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$400(Unknown Source)
        at java.awt.EventQueue$2.run(Unknown Source)
        at java.awt.EventQueue$2.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

1 个答案:

答案 0 :(得分:1)

总体而言,最好使用最新版本5prealpha和http://cmusphinx.sourceforge.net/wiki/tutorialsphinx4

中描述的API

至于你的例外,它说没有找到班级。您需要将该类打包到jar中才能运行代码。包名称也可能有所不同。最近,edu.cmu.sphinx.jsapi包被重命名为edu.cmu.sphinx.jsgf。您可能有问题需要更新。

相关问题