1

I am using spinx4-5prealpha in my application. When I run my application in eclipse, it all works just fine. I used Maven to load the dependencies. When I export the project as a runnable jar file, and run that, i get the following:

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)

All I do is instantiate a new `LiveSpeechRecognizer`

What should i do?

Here's what i dug up so far. So I looked into the code to see where the problem might be. What i've figured out so far is that, the path for the file default.config.xml is what is causing the problem. The path returned by the Matcher used in the ConfigurationManagerUtil is /edu/cmu/sphinx/models/en-us/default.config.xml. Which works fine when it runs in eclipse (though, i dont understand why it does). When i checked the jar file exported, the file it look for is in resources/edu/cmu/sphinx/models/en-us/default.config.xml.
My guess is, the problem is somewhere around here...

Edit

The problem i believe is with the relative paths produced. The sphinx api, at a point uses the getResource(String resource) method, and as i mentioned above, it returns /edu/cmu/sphinx/models/en-us/default.config.xml. But in the jar file i export, these files are actually in the folder lik: /resources/edu/cmu/sphinx/models/en-us/default.config.xml

As a workaround, i copied the resource files i use to a directory in the project itself, and referred to them, and seems it works. Still i would like to know why it misbehaves as described earlier, and what is the proper solution

The .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>

The .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>

The 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>
Ahmed Shariff
  • 793
  • 6
  • 12

3 Answers3

1

To properly create a single executable jar you need to make sure that Eclipse unpacks the dependencies in the right place. The xml file must be in the /edu/cmu/sphinx/api folder inside the jar after packaging. All your paths with en-us are not reasonable because it is not the place where configuration file is expected.

To get further help on this issue you need to provide more information about project layout and the way you use IDE. You seem to confuse maven with eclipse build, you could just use maven.

Nikolay Shmyrev
  • 24,897
  • 5
  • 43
  • 87
  • i dont understand what is meant by project layout or how i use the IDE? – Ahmed Shariff Nov 22 '15 at 15:39
  • I couldn't figure out why eclipse adds the resource files in a folder named `resources` when exported as jar. perhaps it is because there is another directory in the root that is named `edu`, which has the sphinx class files? – Ahmed Shariff Nov 22 '15 at 15:41
  • You need to answer the following question: how do you include sphinx4 dependency. Do you add jars to classpath? Do you add external jars to classpath? Do you import sphinx4 sources and add dependent project? Do you use maven or gradle to build project? You should add jars to classpath, not import sphinx4 project sources as dependent project. When you export the jar do you "Extract required libraries" or do you "Package required libraries". It is also better to use gradle build system instead of Eclipse, it is more reliable build system. – Nikolay Shmyrev Nov 22 '15 at 16:19
  • If you still want to stick with Eclipse, you need to choose "Extract required libraries", not "Package required libraries". – Nikolay Shmyrev Nov 22 '15 at 16:27
  • I built the sphinx source using maven, and imported them as maven projects to eclipse. Then i add them as maven dependencies to my project. and when i export, i extract the required libraries. – Ahmed Shariff Nov 22 '15 at 18:11
  • From the result it seems you add projects as references, not maven dependencies to your main project. Are you sure your main project uses maven? Most likely you are using Eclipse build. – Nikolay Shmyrev Nov 22 '15 at 18:47
  • I am not exactly sure what an eclise build is. what i did was, convert my project to a maven project, then add the sphinx files through maven, that is they were added through `add maven dependencies`. now they are listed in the build path as `maven dependencies`. And then use eclipse's `Export` to get the project as a runnable jar file. – Ahmed Shariff Nov 22 '15 at 19:10
  • 1
    If you use maven then instead of Export you can use maven to build jar as described here: http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven For me it still seems you add projects as references. You can remove sphinx4 projects from workspace and it should still build if you properly added files to maven. You also can share .classpath and .project and pom.xml files from your project to make situation clear. – Nikolay Shmyrev Nov 22 '15 at 19:40
1
  1. Go to sphinx4-5prealpha-src directory on your computer.

  2. To edit the Sphinx API, go to sphinx4-5prealpha-src\sphinx4-core\src\main\java\edu\cmu\sphinx\api directory and edit these files however you like.

  3. Download Gradle on your computer and use Git Bash or Terminal. Make sure you also have a Java compiler that works on your shell.

  4. Use a shell to type the command, making sure you are in the sphinx4-5prealpha directory:

cd sphinx4-5prealpha

build gradle

  1. The command will create .jar files for the source codes that are contained in these folders and if you plug in ALL of these .jar files to your project it will run with the edits that you made to the code.

Get all jar files contained in:

sphinx4-5prealpha/sphinx4-core/build/libs

sphinx4-5prealpha/sphinx4-data/build/libs

0

How are you loading the config file?

You have to use ClassName.class.getResourceAsStream(String pathTOConfigFile) to load any configurations when executing jar files.

When using maven, the src/main/resources folder will be checked to include the resources on the classpath.

Omkar Puttagunta
  • 4,036
  • 3
  • 22
  • 35