Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

java - ClassNotFoundException for included dependency

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...