SaxLoader 中的 Sphinx4 空指针异常
Sphinx4 nullpointerexception in SaxLoader
我在我的应用程序中使用 spinx4-5prealpha。当我 运行 我在 eclipse 中的应用程序时,它一切正常。我使用 Maven 来加载依赖项。当我将项目导出为 运行nable jar 文件时,运行,我得到以下信息:
Exception in thread "recognizerEngine" java.lang.NullPointerException
at edu.cmu.sphinx.util.props.SaxLoader.load(SaxLoader.java:71)
at edu.cmu.sphinx.util.props.ConfigurationManager.<init>(ConfigurationManager.java:58)
at edu.cmu.sphinx.api.Context.<init>(Context.java:59)
at edu.cmu.sphinx.api.Context.<init>(Context.java:44)
at edu.cmu.sphinx.api.AbstractSpeechRecognizer.<init>(AbstractSpeechRecognizer.java:42)
at system.core.RecognizerEngineComponents.LiveSpeechRecognizerExtention.<init>(LiveSpeechRecognizerExtention.java:36)
at system.core.RecognizerEngine.run(RecognizerEngine.java:64)
at java.lang.Thread.run(Unknown Source)
我所做的就是实例化一个新的 `LiveSpeechRecognizer`
我该怎么办?
这是我目前为止挖掘的内容。所以我查看了代码以查看问题可能出在哪里。到目前为止我发现的是,文件 default.config.xml
的路径是导致问题的原因。 ConfigurationManagerUtil
中使用的Matcher
返回的路径是/edu/cmu/sphinx/models/en-us/default.config.xml
。当它在 eclipse 中 运行s 时工作正常(虽然,我不明白为什么会这样)。当我检查导出的 jar 文件时,它查找的文件位于 resources/edu/cmu/sphinx/models/en-us/default.config.xml
.
我的猜测是,问题出在附近...
编辑
我认为问题在于产生的相对路径。狮身人面像 api,在某一点上使用了 getResource(String resource)
方法,正如我上面提到的,它 returns /edu/cmu/sphinx/models/en-us/default.config.xml
。但是在我导出的jar文件中,这些文件实际上在lik文件夹中:/resources/edu/cmu/sphinx/models/en-us/default.config.xml
作为变通方法,我将我使用的资源文件复制到项目本身的一个目录中,并引用它们,看起来它有效。我仍然想知道为什么它会像前面描述的那样行为不端,以及正确的解决方案是什么
.classpath:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="res"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
.project:
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Speech Recognizer</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>fr.obeo.dsl.viewpoint.nature.modelingproject</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpeechRecognizer</groupId>
<artifactId>SpeechRecognizer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.3</source>
<target>1.2</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>edu.cmu.sphinx</groupId>
<artifactId>sphinx4-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>edu.cmu.sphinx</groupId>
<artifactId>sphinx4-data</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>edu.cmu.sphinx</groupId>
<artifactId>sphinx4-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>edu.cmu.sphinx</groupId>
<artifactId>sphinx4-samples</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
您如何加载配置文件?
你必须使用 ClassName.class.getResourceAsStream(String pathTOConfigFile)
在执行 jar 文件时加载任何配置。
使用 maven 时,将检查 src/main/resources
文件夹以包含 classpath
上的资源。
要正确创建单个可执行 jar,您需要确保 Eclipse 在正确的位置解压缩依赖项。 xml 文件必须在打包后的 jar 中的 /edu/cmu/sphinx/api
文件夹中。你所有带 en-us
的路径都不合理,因为它不是预期配置文件的地方。
要获得有关此问题的进一步帮助,您需要提供有关项目布局和使用方式的更多信息IDE。您似乎混淆了 maven 和 eclipse 构建,您可以直接使用 maven。
转到计算机上的 sphinx4-5prealpha-src 目录。
要编辑 Sphinx API,请转到 sphinx4-5prealpha-src\sphinx4-core\src\main\java\edu\cmu\sphinx\api 目录并根据需要编辑这些文件。
在您的计算机上下载 Gradle 并使用 Git Bash 或终端。确保您还有一个适用于 shell 的 Java 编译器。
使用 shell 键入命令,确保您位于 sphinx4-5prealpha 目录中:
cd sphinx4-5prealpha
建设gradle
- 该命令将为这些文件夹中包含的源代码创建 .jar 文件,如果您将所有这些 .jar 文件插入您的项目,它将 运行 包含您对代码。
获取包含在:
中的所有jar文件
sphinx4-5prealpha/sphinx4-core/build/libs
sphinx4-5prealpha/sphinx4-data/build/libs
我在我的应用程序中使用 spinx4-5prealpha。当我 运行 我在 eclipse 中的应用程序时,它一切正常。我使用 Maven 来加载依赖项。当我将项目导出为 运行nable jar 文件时,运行,我得到以下信息:
Exception in thread "recognizerEngine" java.lang.NullPointerException
at edu.cmu.sphinx.util.props.SaxLoader.load(SaxLoader.java:71)
at edu.cmu.sphinx.util.props.ConfigurationManager.<init>(ConfigurationManager.java:58)
at edu.cmu.sphinx.api.Context.<init>(Context.java:59)
at edu.cmu.sphinx.api.Context.<init>(Context.java:44)
at edu.cmu.sphinx.api.AbstractSpeechRecognizer.<init>(AbstractSpeechRecognizer.java:42)
at system.core.RecognizerEngineComponents.LiveSpeechRecognizerExtention.<init>(LiveSpeechRecognizerExtention.java:36)
at system.core.RecognizerEngine.run(RecognizerEngine.java:64)
at java.lang.Thread.run(Unknown Source)
我所做的就是实例化一个新的 `LiveSpeechRecognizer`
我该怎么办?
这是我目前为止挖掘的内容。所以我查看了代码以查看问题可能出在哪里。到目前为止我发现的是,文件 default.config.xml
的路径是导致问题的原因。 ConfigurationManagerUtil
中使用的Matcher
返回的路径是/edu/cmu/sphinx/models/en-us/default.config.xml
。当它在 eclipse 中 运行s 时工作正常(虽然,我不明白为什么会这样)。当我检查导出的 jar 文件时,它查找的文件位于 resources/edu/cmu/sphinx/models/en-us/default.config.xml
.
我的猜测是,问题出在附近...
编辑
我认为问题在于产生的相对路径。狮身人面像 api,在某一点上使用了 getResource(String resource)
方法,正如我上面提到的,它 returns /edu/cmu/sphinx/models/en-us/default.config.xml
。但是在我导出的jar文件中,这些文件实际上在lik文件夹中:/resources/edu/cmu/sphinx/models/en-us/default.config.xml
作为变通方法,我将我使用的资源文件复制到项目本身的一个目录中,并引用它们,看起来它有效。我仍然想知道为什么它会像前面描述的那样行为不端,以及正确的解决方案是什么
.classpath:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="res"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
.project:
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Speech Recognizer</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>fr.obeo.dsl.viewpoint.nature.modelingproject</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpeechRecognizer</groupId>
<artifactId>SpeechRecognizer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.3</source>
<target>1.2</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>edu.cmu.sphinx</groupId>
<artifactId>sphinx4-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>edu.cmu.sphinx</groupId>
<artifactId>sphinx4-data</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>edu.cmu.sphinx</groupId>
<artifactId>sphinx4-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>edu.cmu.sphinx</groupId>
<artifactId>sphinx4-samples</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
您如何加载配置文件?
你必须使用 ClassName.class.getResourceAsStream(String pathTOConfigFile) 在执行 jar 文件时加载任何配置。
使用 maven 时,将检查 src/main/resources
文件夹以包含 classpath
上的资源。
要正确创建单个可执行 jar,您需要确保 Eclipse 在正确的位置解压缩依赖项。 xml 文件必须在打包后的 jar 中的 /edu/cmu/sphinx/api
文件夹中。你所有带 en-us
的路径都不合理,因为它不是预期配置文件的地方。
要获得有关此问题的进一步帮助,您需要提供有关项目布局和使用方式的更多信息IDE。您似乎混淆了 maven 和 eclipse 构建,您可以直接使用 maven。
转到计算机上的 sphinx4-5prealpha-src 目录。
要编辑 Sphinx API,请转到 sphinx4-5prealpha-src\sphinx4-core\src\main\java\edu\cmu\sphinx\api 目录并根据需要编辑这些文件。
在您的计算机上下载 Gradle 并使用 Git Bash 或终端。确保您还有一个适用于 shell 的 Java 编译器。
使用 shell 键入命令,确保您位于 sphinx4-5prealpha 目录中:
cd sphinx4-5prealpha
建设gradle
- 该命令将为这些文件夹中包含的源代码创建 .jar 文件,如果您将所有这些 .jar 文件插入您的项目,它将 运行 包含您对代码。
获取包含在:
中的所有jar文件sphinx4-5prealpha/sphinx4-core/build/libs
sphinx4-5prealpha/sphinx4-data/build/libs