1

I am getting this error java.lang.NoCLassDefFoundError: org/apache/http/HttpEntity when I run my application. It compiles and builds fine. But then I try to use an EJB that imports a HttpClient, I get this error.

As I have read, that I have to import the jars (httpclient-4.5 and httpcore-4.4.5) at runtime. How do I do that?

I have these jars in my gradle script. I would think that the compile would take care of pulling in all of the classes it needs to build and be good to run.

I have a implication of the HttpClient in a class. Below is a slimmed down version of the implementation:

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class httpClientImpl implements httpClient {

    public CloseTrailerResponse closeActionFromAdapter(requestObject request) throws IOException {

        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(trailerCloseAdapterURL);

        try {
            ..setting stuff     
            CloseableHttpResponse response = client.execute(httpPost);      
            }       
        }
        catch(Exception ex) {
            //exceptions logged
        }
       finally {
        client.close();
       }
        return response;
    }
}

This client is instantiated and called in the EJB.

import com.client.httpClientImpl;

private HttpClientImpl httpClientInstance = new HttpClientImpl();
private Request request;
private Response response;

//In a method the http client is called...
response = httpClientInstance.closeActionFromAdapter(request); 

When I access this class at runtime, I get the class def not found.

Is the error that it needs the class at runtime or is it something else? If it needs the class at runtime, how do I add it to the classpath?

Thanks.

Gloria Santin
  • 2,066
  • 3
  • 51
  • 124
  • The answer to this depends on the platform you're trying to access this class from. If the platform can read a "fat jar file", then you could do that. That involves building a jar file that has your own classes and the necessary dependencies all bundled into it. The other option is more what you're asking about. You would independently figure out how to add those classes to your container environment through configuration of some sort. Not knowing what platform you're using, I can't tell you the specifics of any of this. – CryptoFool Feb 21 '19 at 20:39
  • Maybe this answer could offer some insight - https://stackoverflow.com/a/35304025/1098361 – Zack Macomber Feb 21 '19 at 20:41
  • YES. That post seems to be all over this issue. Note that it talks about a "fat jar" via the "Shadow plugin for Gradle". I'm a Maven user, so I don't know about that specifically, but Maven has exactly the same concept. As I said at the start though, this will only work if your platform is able to make use of a fat jar file. I expect that it can. - the fat jar is the way to go so that all of your configuration is localized to your own package and your container doesn't have to know anything about your dependencies. – CryptoFool Feb 21 '19 at 20:44
  • I have found `java.lang.NoClassDefFoundError` runtime errors to be difficult to resolve in the past. What's helped me is isolating/stripping down to a very minimal project and turning on verbose logging output to identify the problem. – Zack Macomber Feb 21 '19 at 20:44
  • True @Zach, but in this case it seems that the OP knows which classes he's missing access to. He just needs to figure out how to get them into the classpath of his EJB. – CryptoFool Feb 21 '19 at 20:45
  • @Zach - that link was helpful..I think I have to add the jar to the EJB – Gloria Santin Feb 21 '19 at 20:57
  • Added the jar to the ear file and still it has the same error. I took the call out of the application and it runs correctly. I am adding the call back in to make sure it is this call that is the problem... – Gloria Santin Feb 22 '19 at 13:38
  • OK. Got a little further. Now, it's complaining about not having the Jackson plugin that we are using to map the objects...I'm optimistic! :) – Gloria Santin Feb 22 '19 at 13:51
  • YEAH! That fixed it! – Gloria Santin Feb 22 '19 at 14:09
  • Guys.... Pull your answer out of the comments so I can accept this as answered. – Gloria Santin Feb 22 '19 at 14:12

0 Answers0