Search code examples
javaapache-sparkapache-spark-sqlstanford-nlp

Why this exception is getting occurred while running the java code with Spark and Stanford NLP?


Here is my code:

try
{
    Dataset<Row> df =  spark.sql("select answer from health where limit 1");

    nameAndCity = df.toJavaRDD().map(new Function<Row, String>() {

     //   @Override
        public String call(Row row) {
            return row.getString(0);
        }
    }).collect();
}

catch (Exception AnalysisException)
{
    System.out.print("\nTable is not found\n");
}
spark.close();
System.out.println("Spark Done....!");
for (String name : nameAndCity) 
{
    Annotation document = new Annotation(name);
    Properties props = new Properties();
    props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse,sentiment");
    //props.setProperty("parse.binaryTrees","true");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    pipeline.annotate(document);
    for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) {
      System.out.println("---");
      System.out.println("Sentence Analyzed: " + " " + sentence.get(CoreAnnotations.TextAnnotation.class));
      System.out.println("Reflected sentiment: " + " " + sentence.get(SentimentCoreAnnotations.SentimentClass.class));
    }
 }

After running this code, I get the following exception:

Exception in thread "main" edu.stanford.nlp.util.ReflectionLoading$ReflectionLoadingException: Error creating edu.stanford.nlp.time.TimeExpressionExtractorImpl
    at edu.stanford.nlp.util.ReflectionLoading.loadByReflection(ReflectionLoading.java:40)
    at edu.stanford.nlp.time.TimeExpressionExtractorFactory.create(TimeExpressionExtractorFactory.java:57)
    at edu.stanford.nlp.time.TimeExpressionExtractorFactory.createExtractor(TimeExpressionExtractorFactory.java:38)
    at edu.stanford.nlp.ie.regexp.NumberSequenceClassifier.<init>(NumberSequenceClassifier.java:86)
    at edu.stanford.nlp.ie.NERClassifierCombiner.<init>(NERClassifierCombiner.java:136)
    at edu.stanford.nlp.pipeline.AnnotatorImplementations.ner(AnnotatorImplementations.java:121)
    at edu.stanford.nlp.pipeline.AnnotatorFactories$6.create(AnnotatorFactories.java:273)
    at edu.stanford.nlp.pipeline.AnnotatorPool.get(AnnotatorPool.java:152)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.construct(StanfordCoreNLP.java:451)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:154)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:150)
    at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:137)
    at spark.sparkhive.queryhive.main(queryhive.java:64)
Caused by: edu.stanford.nlp.util.MetaClass$ClassCreationException: MetaClass couldn't create public edu.stanford.nlp.time.TimeExpressionExtractorImpl(java.lang.String,java.util.Properties) with args [sutime, {}]
    at edu.stanford.nlp.util.MetaClass$ClassFactory.createInstance(MetaClass.java:237)
    at edu.stanford.nlp.util.MetaClass.createInstance(MetaClass.java:382)
    at edu.stanford.nlp.util.ReflectionLoading.loadByReflection(ReflectionLoading.java:38)
    ... 12 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at edu.stanford.nlp.util.MetaClass$ClassFactory.createInstance(MetaClass.java:233)
    ... 14 more
Caused by: java.lang.NoClassDefFoundError: de/jollyday/ManagerParameter
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at edu.stanford.nlp.time.Options.<init>(Options.java:87)
    at edu.stanford.nlp.time.TimeExpressionExtractorImpl.init(TimeExpressionExtractorImpl.java:44)
    at edu.stanford.nlp.time.TimeExpressionExtractorImpl.<init>(TimeExpressionExtractorImpl.java:39)
    ... 19 more
Caused by: java.lang.ClassNotFoundException: de.jollyday.ManagerParameter
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 24 more
17/02/22 18:26:39 INFO ShutdownHookManager: Shutdown hook called
17/02/22 18:26:39 INFO ShutdownHookManager: Deleting directory /tmp/spark-23b5fac2-27ec-44a6-9ff7-cb2b8bfd8753

Why I am getting this exception and what I should do to resolve it?


Solution

  • This one is the source of your trouble:

    java.lang.NoClassDefFoundError: de/jollyday/ManagerParameter
    

    Your program is looking for this class but can't find it. Check your classpath.

    You can ignore the preceding exceptions in the output because they're just reactions to the original exception.

    • ReflectionLoadingException is thrown by a method called loadByReflection, so it seems to be a wrapper exception of some kind and unlikely to tell you much.
    • Then we have ClassCreationException thrown by createInstance, kind of the same situation. In general if you have a method called doSomething that throws DoSomethingException, the exception is either going to tell you exactly what the problem is ("x must be >= 100") or wrap the actual exception.
    • InvocationTargetException is just the exception that the JVM throws when you try to invoke a method using reflection and that method throws an exception. Again it's just a wrapper for the actual exception.
    • Finally we get to the actual exception, NoClassDefFoundError.