4

Issue while using webservice to call some java application method which is using resource file

I have created one java application which contains following directory structure in my src folder of java..

Src/
  Transcriber.java
  config.xml
  digits.gram
  transcriber.manifest

I have successfully created web-service of this java application with .aar file and putting into axis2 service folder

I am packaging this whole in Transcriber.aar file with the following structure

Transcriber\edu\cmu\sphinx\demo\transcriber

and in that all 4 file above I listed.

I have two method in above Transcriber.java class.. 1st method is just processing without any of other file use like (e.g. config.xml,digits.gram and transcriber.manifest). and its working fine and I can call easily that method from android.

but my second method uses the other files also(e.g. config.xml,digits.gram and transcriber.manifest) to process some logic I want.

But some how I'm getting error when I call the second method and its give me error while I'm calling this 2nd method from android device .

My Error is as follows:

  at java.lang.Thread.run(Thread.java:662)
Caused by: Property exception component:'jsgfGrammar' property:'grammarLocation'
 - Can't locate resource:/edu/cmu/sphinx/demo/transcriber
edu.cmu.sphinx.util.props.InternalConfigurationException: Can't locate resource:
/edu/cmu/sphinx/demo/transcriber

its give me error that some how it cant locate the grammar file digits.gram which is i use to add via the config.xml file with this code in config.xml

<component name="jsgfGrammar" type="edu.cmu.sphinx.jsgf.JSGFGrammar">
        <property name="dictionary" value="dictionary"/>
        <property name="grammarLocation" 
             value="resource:/edu/cmu/sphinx/demo/transcriber"/>
        <property name="grammarName" value="digits"/>
    <property name="logMath" value="logMath"/>
    </component>

Why I'm Having this kind of error?enter code here

My CODE WHERE I HAVE FIRST GET THE CONFIG.XML AND THEN CONFIG.XML GET ANOTHER RESOURCE FILE....its successfully finds the config.xml but then the code in config.xml cant locate other resource file

package edu.cmu.sphinx.demo.transcriber;


import edu.cmu.sphinx.frontend.util.AudioFileDataSource;
import edu.cmu.sphinx.recognizer.Recognizer;
import edu.cmu.sphinx.result.Result;
import edu.cmu.sphinx.util.props.ConfigurationManager;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/** A simple example that shows how to transcribe a continuous audio file that has multiple utterances in it. */
public class TranscribeSimpleGrammar {

        private static final String PATH = "file:///D:\\Sound\\";



        @SuppressWarnings({ "null", "null" })               
        public String recognize_wave(String wavePath) throws MalformedURLException{


              URL audioURL;

              //  if (args.length > 0) {
                //    audioURL = new File(args[0]).toURI().toURL();
              //  } else {
                    //audioURL = TranscribeSimpleGrammar.class.getResource("hello.wav");
                        //audioURL = new URL(PATH + "turn-on-light-kitchen-male.wav");
                        //audioURL = new URL(PATH + "turn-down-tv-volume-female.wav");
                       // audioURL = new URL(PATH + wavePath);
              audioURL = new URL(wavePath);
              //audioURL = new URL(PATH + "turn-down-dining-room-music-player-volume-male.wav");

               // }

                URL configURL = TranscribeSimpleGrammar.class.getResource("config.xml");

                ConfigurationManager cm = new ConfigurationManager(configURL);
                Recognizer recognizer = (Recognizer) cm.lookup("recognizer");

                /* allocate the resource necessary for the recognizer */
                recognizer.allocate();

                // configure the audio input for the recognizer
                AudioFileDataSource dataSource = (AudioFileDataSource) cm.lookup("audioFileDataSource");
                dataSource.setAudioFile(audioURL, null);

                // Loop until last utterance in the audio file has been decoded, in which case the recognizer will return null.
                Result result;
                while ((result = recognizer.recognize())!= null) {

                        String resultText = result.getBestResultNoFiller();
                        System.out.println(resultText);
                }

            return result.getBestResultNoFiller();
        }

        public String get_wav_byte(byte[] wavbite,String path){

            String result1="null";

            try
            {
                File dstFile = new File(path);
                FileOutputStream out = new FileOutputStream(dstFile);
                out.write(wavbite, 0, wavbite.length);

                out.close();

            }
            catch (IOException e)
            {
              System.out.println("IOException : " + e);
            }


            try {
                result1=recognize_wave(path);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


         return result1;


     }




}
BhavikKama
  • 8,566
  • 12
  • 94
  • 164
  • 3
    You already asked that question earlier (http://stackoverflow.com/questions/12436173/webservice-giving-error-like-cant-locate-resources-when-using-some-dependet-file) but you deleted it (including my answer to that question). Why did you do that? – Andreas Veithen Sep 27 '12 at 06:43
  • i am using that account not usual now..and as i want to put bounty on this question so thats why.Sorry for that – BhavikKama Sep 27 '12 at 07:35
  • @AndreasVeithen you mention that you have already answered this question? did your answer solve bhaviks problem? – Mukul Goel Oct 03 '12 at 17:30
  • no yet not.that's why i have put bounty because this work is rarely done.so my question has not received enough attention – BhavikKama Oct 04 '12 at 05:02
  • 1
    @Mukul: I pointed out the likely cause of the issue. The question is whether he actually did anything to validate (or rule out) that explanation. That would have been the precondition for finding a solution to the problem. – Andreas Veithen Oct 04 '12 at 18:14
  • hmmmm... bhavik can you post the code of the method you are having probkem with ? – Mukul Goel Oct 04 '12 at 18:27
  • @AndreasVeithen brother can you please share that again? :-) – Mukul Goel Oct 04 '12 at 18:27
  • @MUKUL GOEL Please See my code where the config.xml file is read first and that in that config file there is code to read another resources files..what i want to say that Andreas Veithen has said that it cant locate the resource file because of class loader issue.but its successful locate the config.xml file at once and then it cant locate other resources from config.xml file.so it will not be class loader issue – BhavikKama Oct 05 '12 at 06:05
  • 3
    If you summarize my original answer by saying that "it cant locate the resource file because of class loader issue", then you didn't read that answer carefully. – Andreas Veithen Oct 05 '12 at 11:18
  • @bhavik , one class is loaded and other is not loaded , this is your premise to say it is not a class loader issue. if it can not find a class/ having problem loading( because of duplicacy etc etc) it is a class loader issue. 1 billion files could be loaded fine ,but if 1 is not being loaded, it can still be a classloader issue, regards. – Mukul Goel Oct 05 '12 at 18:10
  • @bhavik, please update the post with structure of your aar file . explanation is not making much sense to me. please post a tree structure.regards – Mukul Goel Oct 05 '12 at 18:15
  • Under what package does transcriber exist? – Pradeep Pati Apr 22 '13 at 03:01
  • its in /edu/cmu/sphinx/demo/transcriber – BhavikKama Apr 22 '13 at 03:44
  • Andreas answer is true, due to the bug in s4 it's not possible to load folder with tomcat classloader. This issue has to be fixed in s4 itself. You need to be careful about deleting valid answers and about using multiple accounts to upvote a question – Nikolay Shmyrev Sep 28 '13 at 18:35

1 Answers1

1

Have you ever tried to change

<property name="grammarLocation" value="resource:/edu/cmu/sphinx/demo/transcriber"/>

to

<property name="grammarLocation" value="resource:edu/cmu/sphinx/demo/transcriber"/>

(meaning just removing the leading slash before edu)?

Class.getResource() and ClassLoader.getResource() do interpret the provided name differently: while Class.getResource( "/edu/cmu/sphinx/demo/transcriber/transcriber.manifest" ) would find the resource, you have to use ClassLoader.getResource() with the argument "edu/cmu/sphinx/demo/transcriber/transcriber.manifest" to find the same resource. As you do not know which method is used by the library code you call to load the other resources, you should give it a try.

tquadrat
  • 3,033
  • 1
  • 16
  • 29