简体   繁体   中英

How to resolve runtime Eclipse NoClassDefFoundError

I'm experimenting with writing an openHab2 binding which is written in java. I'm a C++ guy and java is new to me. The offending code looks like this:

import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
...
@Override
public void initialize() {
    ...
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    ...
}

I added org.apache.httpcomponents.htpclient_4.5.2.v20170210-0925.jar to the buildpath as an external jar and the program builds without any problems. This project uses Maven, which I'm not familiar with either, as the build system so I added:

  <dependencies>
    <dependency>
        <groupId>org.apache.httpcomponent</groupId>
        <artifactId>httpclient</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <scope>runtime</scope>
    </dependency>
  </dependencies>

to the pom.xml.

When I run the system I get this error:

2018-05-20 09:51:38.574 [ERROR] [.icAbstractInvocationHandler:101 ] - An error occurred while calling method 'ThingHandler.initialize()' on 'org.openhab.binding.testbinging.internal.TestBingingHandler@728656fc': org/apache/http/impl/client/BasicCredentialsProviderjava.lang.NoClassDefFoundError: org/apache/http/impl/client/BasicCredentialsProvider at org.openhab.binding.testbinging.internal.TestBingingHandler.initialize(TestBingingHandler.java:63)

2018-05-20 09:51:38.576 [ERROR] [.c.thing.internal.ThingManager:700 ] - Exception occurred while initializing handler of thing 'testbinging:sample:0ac3dcf3': org/apache/http/impl/client/BasicCredentialsProviderjava.lang.NoClassDefFoundError: org/apache/http/impl/client/BasicCredentialsProvider

It look to my untrained eye like the runtime classpath is not set correctly.

I'm using eclipse-oxygen version Oxygen.3a Release(4.7.3a), Build id: 20181405.1200 and $ mvn -version Apache Maven 3.3.9 Maven home: /usr/share/maven Java version: 1.8.0_171, vendor: Oracle Corporation Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre

Thanks, Steve Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "4.9.0-5-amd64", arch: "amd64", family: "unix"

It's because your dependencies are runtime:

<scope>runtime</scope>

You need to either change it to:

 <scope>compile</scope>

Or delete the scope line, since this is the default scope.

Runtime dependencies aren't used at compile time. You can't read more about it: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM