Java – 如何导入外部包?

我还是比较新的Java。 我已经尝试过寻找解决方案,但我认为我还不知道足够的Java甚至不知道我应该搜索什么。

我想尝试使用语音识别,所以我下载了CMU的Sphinx-4源代码并进行了编译。 一旦我完成了所有工作,我就可以运行所包含的演示了。 接下来,我在保存Sphinx-4目录的目录中为我的代码创建了一个目录(/ Jarvis /)。 在Sphinx-4源目录中,有一长串目录导致我玩过的演示(/Sphinx4-1.0beta6/src/apps/edu/cmu/sphinx/demo/)。 在demo文件夹中是一个HelloWorld目录,其中包含使用Sphinx-4函数的简单程序的源代码。 我的第一个目标是在我为自己的代码设置的目录中获取此演示的副本。 所以我已经制作了HelloWorld文件的副本并将它们放在我的Jarvis目录中。 重命名并更改了代码,以便以前是HelloWorld的所有内容现在都是Jarvis。 不幸的是,我遇到的问题并不像命名问题那么简单,而是找到包的路径。

使用Java,我大多只使用简单的“javac example.java”命令来编译代码。 我猜我在这种情况下可能需要更多东西,但我不确定究竟是什么。 当尝试以这种方式编译时,我得到错误:

Jarvis.java:15: error: package edu.cmu.sphinx.frontend.util does not exist import edu.cmu.sphinx.frontend.util.Microphone; ^ Jarvis.java:16: error: package edu.cmu.sphinx.recognizer does not exist import edu.cmu.sphinx.recognizer.Recognizer; ^ Jarvis.java:17: error: package edu.cmu.sphinx.result does not exist import edu.cmu.sphinx.result.Result; ^ Jarvis.java:18: error: package edu.cmu.sphinx.util.props does not exist import edu.cmu.sphinx.util.props.ConfigurationManager; ^ Jarvis.java:28: error: cannot find symbol ConfigurationManager cm; ^ symbol: class ConfigurationManager location: class Jarvis Jarvis.java:31: error: cannot find symbol cm = new ConfigurationManager(args[0]); ^ symbol: class ConfigurationManager location: class Jarvis Jarvis.java:33: error: cannot find symbol cm = new ConfigurationManager(Jarvis.class.getResource("jarvis.config.xml")); ^ symbol: class ConfigurationManager location: class Jarvis Jarvis.java:36: error: cannot find symbol Recognizer recognizer = (Recognizer) cm.lookup("recognizer"); ^ symbol: class Recognizer location: class Jarvis Jarvis.java:36: error: cannot find symbol Recognizer recognizer = (Recognizer) cm.lookup("recognizer"); ^ symbol: class Recognizer location: class Jarvis Jarvis.java:40: error: cannot find symbol Microphone microphone = (Microphone) cm.lookup("microphone"); ^ symbol: class Microphone location: class Jarvis Jarvis.java:40: error: cannot find symbol Microphone microphone = (Microphone) cm.lookup("microphone"); ^ symbol: class Microphone location: class Jarvis Jarvis.java:53: error: cannot find symbol Result result = recognizer.recognize(); ^ symbol: class Result location: class Jarvis 12 errors 

所以我想要包括那些丢失的包。 我知道它与路径在代码中的定义方式有关,但我还不了解Java还知道如何定义这些路径。 我想在代码中更改一些内容吗? 我应该以不同的方式预编译吗? 只是推动正确的方向将是有用的。

下面我已经包含了最初来自HelloWorld演示的4个文件,我对其进行了轻微的命名修改。

感谢您的时间!

Jarvis.java:

 package jarvis.jarvis; 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; /** * A simple HelloWorld demo showing a simple speech application built using Sphinx-4. This application uses the Sphinx-4 * endpointer, which automatically segments incoming audio into utterances and silences. */ public class Jarvis { public static void main(String[] args) { ConfigurationManager cm; if (args.length > 0) { cm = new ConfigurationManager(args[0]); } else { cm = new ConfigurationManager(Jarvis.class.getResource("jarvis.config.xml")); } Recognizer recognizer = (Recognizer) cm.lookup("recognizer"); recognizer.allocate(); // start the microphone or exit if the programm if this is not possible Microphone microphone = (Microphone) cm.lookup("microphone"); if (!microphone.startRecording()) { System.out.println("Cannot start microphone."); recognizer.deallocate(); System.exit(1); } System.out.println("Say: (Good morning | Hello) ( Bhiksha | Evandro | Paul | Philip | Rita | Will )"); // loop the recognition until the programm exits. while (true) { System.out.println("Start speaking. Press Ctrl-C to quit.\n"); Result result = recognizer.recognize(); if (result != null) { String resultText = result.getBestFinalResultNoFiller(); System.out.println("You said: " + resultText + '\n'); } else { System.out.println("I can't hear what you said.\n"); } } } } 

jarvis.config.xml:

                        accuracyTracker  speedTracker  memoryTracker                                                                             microphone  preemphasizer  windower  fft  melFilterBank  dct  liveCMN  featureExtraction         microphone  dataBlocker  speechClassifier  speechMarker  nonSpeechDataFilter  preemphasizer  windower  fft  melFilterBank  dct  liveCMN  featureExtraction        <!---->                                                 

jarvis.gram:

 #JSGF V1.0; /** * JSGF Grammar for Hello World example */ grammar jarvis; public  = (Good morning | Hello) ( Bhiksha | Evandro | Paul | Philip | Rita | Will ); 

jarvis.Manifest

 Main-Class: jarvis.Jarvis Class-Path: ../sphinx4-1.0beta6/lib/sphinx4.jar ../sphinx4-1.0beta6/lib/WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar 

编译器使用“类路径”来查找所需的库。 设置类路径的方法有很多种:一种是使用CLASSPATH环境变量,另一种是使用-classpath命令行开关。 总之,您可以编译:

 javac -classpath path/to/sphinx.jar jarvis/Jarvis.java 

您可以在http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html找到完整的文档。

另一件事:你的Jarvis类声明它在一个名为jarvis.jarvis的包中,所以jarvis.jarvis的完全限定名称是jarvis.jarvis.Jarvis …不确定这是你想要的。

使用“-classpath”选项告诉javac在哪里可以找到所有需要的类。

 javac -classpath lib/blablabla.jar;lib/foobar.jar 

设置类路径 。