将 CMU Sphinx 4 与 Maven 项目一起使用时出现 UnsupportedClassVersionError

UnsupportedClassVersionError while using CMU Sphinx 4 with maven project

我正在使用 sphinx4 实现语音转文本,我的代码如下:

  public class TranscriberDemo {       

        public static void main(String[] args) throws Exception {

            Configuration configuration = new Configuration();

            configuration
                    .setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");
            configuration
                    .setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");
            configuration
                    .setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin");

            StreamSpeechRecognizer recognizer = new StreamSpeechRecognizer(
                    configuration);
            InputStream stream = new FileInputStream(new File("BrookBentonAMillionMilesFromNowhere.wav"));

            recognizer.startRecognition(stream);
            SpeechResult result;
            while ((result = recognizer.getResult()) != null) {
                System.out.format("Hypothesis: %s\n", result.getHypothesis());
            }
            recognizer.stopRecognition();
        }
    }

我收到以下错误:

Exception in thread "main" java.lang.UnsupportedClassVersionError: edu/cmu/sphinx/api/Configuration : Unsupported major.minor version 52.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access0(URLClassLoader.java:71)
    at java.net.URLClassLoader.run(URLClassLoader.java:361)
    at java.net.URLClassLoader.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at com.pericent.test.TranscriberDemo.main(TranscriberDemo.java:16)

我的pom.xml如下:

 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- servlet api jar for servlets -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>

        <dependency>
          <groupId>edu.cmu.sphinx</groupId>
          <artifactId>sphinx4-core</artifactId>
          <version>5prealpha-SNAPSHOT</version>
        </dependency>
        <dependency>
          <groupId>edu.cmu.sphinx</groupId>
          <artifactId>sphinx4-data</artifactId>
          <version>5prealpha-SNAPSHOT</version>
        </dependency>
  </dependencies>
   <repositories>
        <repository>
            <id>snapshots-repo</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
            <releases><enabled>false</enabled></releases>
        <snapshots><enabled>true</enabled></snapshots>
        </repository>
    </repositories>

我正在使用 java7,无法升级到 java8。我也试图找到旧版本的 Sphinx 4 但没有成功。那么我可以解决这个问题吗?

您的代码是使用 Java 8 编译的,目标版本为 Java 8。因此,它不能 运行 on Java 7(可以仅支持 class 版本最高为 51 (details here) 的文件。

您需要为 Java 7 编译代码,使用:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
      <source>1.7</source>
      <target>1.7</target>
    </configuration>
</plugin>

来自 here

或通过设置

<properties>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>

在主要属性中。

我刚刚在 sphinx4 中修复了它,现在它强制执行 java 1.7。我还将更新的 jars 部署到 Maven,你只需更新它就可以了。