ClassNotFoundException:javax.speech.EngineModeDesc

时间:2015-07-23 12:58:50

标签: java classnotfoundexception javax.speech

以下是我在java中编写实现Text to Speech的代码 使用freeTTS和mbrola。

我已将所有jar的freeTTS添加到我的类路径中。

import java.applet.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.speech.Central;
import javax.speech.EngineList;
import javax.speech.EngineCreate;
import javax.speech.synthesis.SynthesizerModeDesc;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.Voice;
import com.sun.speech.freetts.jsapi.FreeTTSEngineCentral;
import java.util.Locale;
import java.awt.Event;
public class voisedemo extends Applet implements ActionListener
{

 public Synthesizer synth;
 private static Voice kevinHQ;
 TextField t1;

 public void init()
 {
 Button b1 = new Button("press me");
 add(b1);
 b1.addActionListener(this);
 t1 = new TextField(50);
 add(t1);

 }
 public void start()
 {
 }


 public void actionPerformed(ActionEvent e)
 {
 // synthesizer.speakPlainText(“Hello, world!”, null);
 try {
 setKevinHQ(new Voice("Hitesh",
 Voice.AGE_NEUTRAL,
 Voice.GENDER_MALE,
 null ));


 System.setProperty("mbrola.base", "C:/Users/Sai/Downloads/mbrola");
 SynthesizerModeDesc modeDesc = new SynthesizerModeDesc(
 null,
 "general", /* use “time” or “general” */
 Locale.US,
 Boolean.FALSE,
 null);
 FreeTTSEngineCentral central = new FreeTTSEngineCentral();
 Synthesizer synthesizer = null;
 synthesizer = Central.createSynthesizer( modeDesc );
 EngineList list = central.createEngineList(modeDesc);
 if (list.size() > 0) {
 EngineCreate creator = (EngineCreate) list.get(0);
 synthesizer = (Synthesizer) creator.createEngine();
 }

 if (synthesizer == null) {
 System.err.println("Cannot create synthesizer");
 System.exit(1);
 }
 //get ready to speak
 synthesizer.allocate();
 synthesizer.resume();
 String s1 = t1.getText();
 synthesizer.speakPlainText(s1, null);
 synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
 synthesizer.deallocate();
 } catch (Exception eq) {
 eq.printStackTrace();
 }
 }
 public static void setKevinHQ(Voice kevinHQ) {
 voisedemo.kevinHQ = kevinHQ;
 }
 public static Voice getKevinHQ() {
 return kevinHQ;
 }
 public void paint(Graphics g)
 {
 }
}

我使用以下命令在命令提示符中编译它:

C:\Users\Sai\Desktop\Mini Project>javac voisedemo.java

没有编译错误。

但是我什么时候运用它来运行:

C:\Users\Sai\Desktop\Mini Project>java voisedemo

我得到以下运行时错误:

Error: A JNI error has occurred, please check your installation and try   again
    Exception in thread "main" java.lang.NoClassDefFoundError: javax/speech/EngineModeDesc
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
    Caused by: java.lang.ClassNotFoundException: javax.speech.EngineModeDesc
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 7 more

这个程序是我迷你项目的一部分。

请帮我解决这个错误。

2 个答案:

答案 0 :(得分:0)

您丢失了voce发行版提供的jsapi.jar文件。

答案 1 :(得分:-1)

您需要指定类路径:

java -cp (fullpathifnotincurrentworkingdirectory)yourjarfile.jar;. voicedemo

摘自here