0

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] [.i.c.AbstractInvocationHandler: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"

zephod
  • 11
  • 1
  • So you did both, added the JAR and used Maven? Try to remove the JAR and use Maven only (maybe there is a version mismatch between the interface and the implementation JARs). How do you exactly run the system? – howlger May 21 '18 at 08:18
  • I removed the external jars but problems remain. See the comments in the answer below. – zephod May 22 '18 at 03:39

1 Answers1

0

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

Mirna De Jesus
  • 199
  • 1
  • 4
  • But its a runtime error, not a compile time error. The build is OK. I have tried with no scope at all and got the same result. I'll check out the link you posted. – zephod May 20 '18 at 23:04
  • If you are using maven, you won't need to include dependencies as external jars. Dependencies you include in the pom will be added to your classpath. I think the first thing is removing the external jar from the classpath, then adding the scope and version of each dependency to your pom. Can you try that ? org.apache.httpcomponents httpclient 4.5.5 – Mirna De Jesus May 21 '18 at 00:00
  • If I remove the external jar from the build path, then clean and rebuild the project, I get build errors. I tried running anyhow but got the same runtime error as before. – zephod May 21 '18 at 01:26
  • Did you add the version part?? What are the build errors you are getting? – Mirna De Jesus May 21 '18 at 02:49
  • Yes, I added the version part. I get "The import org.apache cannot be resolved" for each of the import lines and "BasicCredentialsProvider cannot be resolved to a type" and "CredentialsProvider cannot be resolver to a type". – zephod May 21 '18 at 04:26
  • If you have a compilation issue is either, the declaring dependency is not being resolved or you are declaring the wrong dependency. Did you update the maven project after those changes?? https://stackoverflow.com/questions/2555845/how-to-update-maven-repository-in-eclipse/24509023 – Mirna De Jesus May 21 '18 at 12:32
  • I was looking around for how to "update the maven project" and I discovered some command line maven commands, in particular "mvn compile" which takes Eclipse out of the picture and provides much more detailed output. So running "mvn compile" shows that build succeeded. What's strange is that there is no mention of httpclient in the output. If I try to build from the Eclipse, I still get the same compile time errors as before. – zephod May 22 '18 at 01:40
  • Well, I wrote too soon. If I run "mvn clean" before "mvn compile" that I get the errors again. However I think I have found the culprit. If I run "mvn -X compile", I see this in the output: "!ENTRY org.eclipse.equinox.p2.publisher.eclipse 4 0 2018-05-22 07:23:49.882 !MESSAGE Unable to acquire PluginConverter service during generation for: /home/steve/.m2/repository/org/apache/httpcomponents/httpclient/4.5.5/httpclient-4.5.5.jar." – zephod May 22 '18 at 11:32