ClassNotFoundException for included dependency
Asked Answered
G

1

3

I'm using maven to include the google gdata module in my project and everything compiles fine, but I get this runtime exception:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gdata/util/ServiceException
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2570)
    at java.lang.Class.getMethod0(Class.java:2813)
    at java.lang.Class.getMethod(Class.java:1663)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: com.google.gdata.util.ServiceException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 6 more

I'm including the module in my pom like so...

...
<dependencies>
    <dependency>
        <groupId>com.google.gdata</groupId>
        <artifactId>core</artifactId>
        <version>1.47.1</version>
    </dependency>
</dependencies>
...

And including in my class like so...

...
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.docs.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.ListEntry;
import com.google.gdata.data.spreadsheet.ListFeed;
import com.google.gdata.util.ServiceException;
...

When I open the dependencies and dive into the jars, I see the ServiceException class... so what gives? I tried downloading the sources just for fun, but didn't help with anything.

I'm sure it's something silly I'm doing wrong...

--- Update ---

I checked for duplicate dependencies like suggested in the comments. After finding that one of the dependencies was being included with different versions, I updated the pom file:

<dependency>
    <groupId>com.google.gdata</groupId>
    <artifactId>core</artifactId>
    <version>1.47.1</version>
    <type>jar</type>
    <exclusions>
        <exclusion>
            <artifactId>jsr305</artifactId>
            <groupId>com.google.code.findbugs</groupId>
        </exclusion>
    </exclusions>
</dependency>

There's still a version discrepancy for guava, but even excluding guava entirely doesn't break the build (and the dependency tree only marks it as a warning) and I still get the same error, so I don't think that's the problem. The class is in that gdata core module anyway...

Literally the only other class I have in my project is a pretty sparse enum class, and I've listed all non core-java classes that I include.

I've cleaned, deleted .m2 built, rebuilt, same problem.

Gizela answered 24/9, 2016 at 11:32 Comment(4)
try with cleaning project,maven updateTrafficator
this can happen, if you have 2 different versions of the library, or libraries with the same name, try to clean the project, check if you maybe include something twiceEjaculate
Or some dependecy already includes this one, so you would need to exclude it from there and defined it explicitly as you already did.Tours
What y'all said might still be the problem, but I've checked all the ways I know how to check (talked about in the update) :/.Gizela
L
1

Obviously, com.google.gdata.util.ServiceException class can't be found under your classpath.

  1. Try to pull the dependencies again, from command-line and from your project folder: dependency:copy-dependencies
  2. Try to sync your eclipse IDE by refreshing the project, and/or by m2e eclipse plugin by update dependencies.
  3. On command-line & from your project folder try to compile again using: mvn clean compile
  4. If that doesn't work, look for the unfound class under Maven dependencies in your project, (you should be able to find)
  5. Make sure your Maven dependencies are a part of your build configuration. Try to compile again: mvn clean compile

If you have the requested dependency in your Local Maven Repository, and it's included in your project, and your build configuration includes the Maven dependencies to be included in your output classpath folder. Then it should work.

Lexine answered 25/9, 2016 at 12:47 Comment(1)
Turns out my dependencies weren't part of the build configuration. Thanks!Gizela

© 2022 - 2024 — McMap. All rights reserved.