将jar添加到类路径以满足运行时要求

时间:2019-02-21 20:34:25

标签: java httpclient

我在运行应用程序时收到此错误 java.lang.NoCLassDefFoundError:org / apache / http / HttpEntity 。它可以编译并构建良好。但是然后我尝试使用导入HttpClient的EJB,却出现此错误。

如我所读,我必须在运行时导入jar(httpclient-4.5和httpcore-4.4.5)。我该怎么办?

我的gradle脚本中有这些罐子。我认为编译将负责提取它需要构建的所有类,并很好地运行。

我在一个类中暗示了HttpClient。以下是实施的精简版:

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;
    }
}

此客户端已实例化并在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); 

在运行时访问此类时,得到的类def未找到。

是在运行时需要该类的错误还是其他错误? 如果它在运行时需要该类,该如何将其添加到类路径中?

谢谢。

0 个答案:

没有答案