问题
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.
- Try to pull the dependencies again, from command-line and from your project folder:
dependency:copy-dependencies
- Try to sync your eclipse IDE by refreshing the project, and/or by m2e eclipse plugin by update dependencies.
- On command-line & from your project folder try to compile again using:
mvn clean compile
- If that doesn't work, look for the unfound class under Maven dependencies in your project, (you should be able to find)
- 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