ClassNotFoundException during smartsheet login

2019-08-19 22:46发布

问题:

I'm trying to login to smartsheet using their api but I'm encountering a NoClassDefFoundError caused by a ClassNotFound Exception.

import java.util.ArrayList;
import java.util.Properties;
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.io.InputStream;
import java.io.FileInputStream;
import com.smartsheet.api.Smartsheet;
import com.smartsheet.api.SmartsheetFactory;
import com.smartsheet.api.models.Cell;
import com.smartsheet.api.models.Column;
import com.smartsheet.api.models.Row;
import com.smartsheet.api.models.Sheet;
import com.smartsheet.api.models.PagedResult;
import com.smartsheet.api.models.AutomationAction;
import com.google.gson.Gson;
import com.smartsheet.api.SmartsheetBuilder;

public class Main 
{
    //main method for our main class
    public static void main(String args[]) throws Exception
    {
        /*line that throws error*/
        Smartsheet ss = new SmartsheetBuilder().setAccessToken("myAccessToken").build(); 
        // other parts of the program
        return;
    }
}

Stacktrace:

    Exception in thread "main" java.lang.NoClassDefFoundError: 
    com/fasterxml/jackson/databind/JsonDeserializer
        at com.smartsheet.api.internal.SmartsheetImpl.
    (SmartsheetImpl.java:279)
        at com.smartsheet.api.SmartsheetBuilder.build(SmartsheetBuilder.java:258)
        at smartsheet.Main.main(Main.java:27)
    Caused by: java.lang.ClassNotFoundException: 
    com.fasterxml.jackson.databind.JsonDeserializer
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 3 more

Is there any particular reason why this error is thrown? I've tried to access smartsheet using smartsheetfactory as demonstrated in their sample program, but I'm getting the same error.

回答1:

NoClassDefFoundError occurs when the class was present during Compile time but not available during Runtime for any reason.

It is really hard to diagnose and fix this problem as the problem is unavailability of the class file at runtime in the classpath. Thy these point to solve the NoClassDefFoundError

  1. Make sure if the class or jar containing that class is available in the classpath.
  2. If it’s available on application’s classpath then most probably classpath is getting overridden. To fix that you need to find the exact classpath used by your application.
  3. If an application is using multiple class loaders then classes loaded by one classloader may not be available by other class loaders.

In J2EE environment it might be a case when one get NoClassDefFoundError even if the class is present because it may not be visible to the corresponding class loader.



回答2:

The reason why I got this error was because I had the wrong JAR file for the API in my build. The JsonSerializer class was a dependency, and the correct JAR file included dependencies.