Java Maven项目中FreeTTS代码运行异常:词典加载失败求助
Hey, I've run into this exact FreeTTS lexicon issue before when building voice assistant apps—let's break down how to fix it step by step:
1. Fix Your Maven Dependencies First
Most old tutorials skip the critical CMU Lexicon dependency, which is exactly what's causing your Can't load lexicon error. Here's the correct pom.xml setup:
First, add the core FreeTTS and CMU Lexicon dependencies:
<dependencies> <!-- FreeTTS Core Library --> <dependency> <groupId>com.sun.speech</groupId> <artifactId>freetts</artifactId> <version>1.2.2</version> </dependency> <!-- CMU Lexicon (Required for voice data) --> <dependency> <groupId>com.sun.speech</groupId> <artifactId>freetts-cmulex</artifactId> <version>1.2.2</version> </dependency> </dependencies>
If Maven can't find these artifacts, add this repository to your pom.xml (since some FreeTTS jars aren't in Maven Central):
<repositories> <repository> <id>java-net-maven2</id> <name>Java.net Maven Repository</name> <url>http://download.java.net/maven/2/</url> </repository> </repositories>
2. Update Your Code to Use a Valid Voice
Even with correct dependencies, if you don't specify a voice that includes lexicon data, FreeTTS will throw that error. Use one of the pre-packaged CMU voices like kevin16 (16kHz, full lexicon support):
import com.sun.speech.freetts.Voice; import com.sun.speech.freetts.VoiceManager; public class JarvisTTS { public static void main(String[] args) { // Grab the CMU Kevin voice (includes the required lexicon) Voice voice = VoiceManager.getInstance().getVoice("kevin16"); if (voice == null) { System.err.println("Couldn't locate the Kevin voice—check your dependencies!"); System.exit(1); } voice.allocate(); voice.speak("Hello, I'm your Jarvis-like assistant."); voice.deallocate(); } }
3. Troubleshoot Local Repository Issues
If you still get the error, your local Maven jar might be corrupted:
- Delete the
com/sun/speechfolder from your local Maven repository (usually~/.m2/repository/com/sun/speech) - Run
mvn clean installto re-download fresh, complete copies of the jars - Verify that
freetts-cmulex-1.2.2.jarcontains thecmulexdirectory with files likecmudict.0.7a—if not, the jar is incomplete.
4. Consider a More Modern Alternative
FreeTTS is pretty outdated at this point. If you keep hitting roadblocks, check out MaryTTS (open-source, active maintenance) or cloud-based APIs like Google TTS—they have better Maven support and fewer legacy issues.
内容的提问来源于stack exchange,提问作者Ali Hassan

