ClassNotFoundException for included dependency

落花浮王杯 提交于 2019-11-27 04:56:23

问题


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.


回答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.



来源:https://stackoverflow.com/questions/39675845/classnotfoundexception-for-included-dependency

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!