Apache HTTPClient dependency issue : POM - java

I'm having issue while using Apache HttpClient in my java application.
2019-02-11 07:09:18,270 ERROR [Call-Dequeue-Delegator] (HibernateUtil.java:275) - Building SessionFactory failed.
java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpRequestBase
at java.lang.Class.getDeclaredMethods0(Native Method)
It runs fine on my local machine but not on servers. Since it is a client application I'm not having details of server etc.
Following is my Code:
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(20)
.setConnectionRequestTimeout(30).build();
// Creating client with request configuration(timeouts) & disabling redirect following
CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).disableRedirectHandling().build();
String endpoint = this.getSOAPEndPoint();
String queryParamWithSurveyData = addDataToQueryParam(endpoint, customRequestModel);
endpoint += queryParamWithSurveyData;
HttpRequestBase httpRequestWithoutBody = null; // Failing at this
In the above code it's failing at the last line & I'm surprised why not on first one since both (RequestConfig & HttpRequestBase) are imported from HTTPClient.jar
Following is my POM snippet
//Existing sample dependency
<dependency> //Existing sample dependency
<groupId>com.connectfirst.intelliqueue</groupId>
<artifactId>gson</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/couchbase/gson-2.3.jar</systemPath>
<version>1</version>
</dependency>
// New dependencies added
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpcore-4.4.11.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpclient-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
I saw somewhere that HttpClient is dependent on some other JARs as well & therefore added the following dependencies as well but had no luck.
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpcore-4.4.11.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpclient-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/commons-codec-1.11.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/commons-logging-1.2.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/fluent-hc-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-cache</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpclient-cache-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-osgi</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpclient-osgi-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-win</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpclient-win-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/httpmime-4.5.7.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jna-4.5.2.jar</systemPath>
<version>1</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jna-platform-4.5.2.jar</systemPath>
<version>1</version>
</dependency>
The JAR gets created successfully on my local with maven clean install.
Any lead would be helpful.
Thanks.

Why are you scoping to system? That would imply that the jars are on the classpath of the running application. If you switch that to compile, assuming you're packaging as a spring-boot JAR or WAR file, they will be included in the resultant artifact. Without that the application cannot find the dependencies you are working with.
The scope attribute dictates where dependencies should be placed on the classpath.
compile - the artifact is available for reference by the project code and should be bundled (ostensibly) with the resultant artifact (e.g. embedded in WAR under WEB-INF/lib or spring-boot as BOOT-INF/lib)
provided - the artifact is available for reference by the project source code but is not included in the resultant artifact (think JSP libraries in a web app, you want to defer to the container implementation).
runtime - the dependency classes are not available for direct reference by the project source code but is referenced at runtime (think JDBC driver, you don't specifically use that you load it, traditionally, by reflection using the name).
test - the dependency classes are available to sources in src/test/java (etc). IDEs are supposed to separate the references but not always the case (e.g. Eclipse). If you try to reference a test scoped dependency from src/main/java your code will not compile because the dependency is not available.
system - The artifact in this case is explicitly located by path and is expected to be available at runtime within the consuming application's classpath.
import - This is only supported within the dependencyManagement elements.
You can see the description from the official documentation.

Please make sure you are using correct java version on server.
Please note that as of 4.4, HttpClient requires Java 1.6 or newer. for httpClent after 4.4 version you need to use newer java versions (say java 11) .

Related

Maven Spring Boot 2.0.4 - and elastic search 6.2.2 transport jar not in dependency tree

I am writing a SpringBoot 2.0.4 application, (Executable .jar) and I am trying to interface with an elastic search that I do not control. It is 6.2.2
Now I know Spring does not support that, and that's fine as I am not using any Spring classes, I am interfacing with elastic directly. However, The problem comes in trying to exclude elastic from spring in my pom. Here is my pom fragment:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.2.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.2.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.2.2</version>
</dependency>
Now Eclipse has a yellow line under the versions for elasticsearch and transport, saying that the version is being overridden. But when I look at the dependency tree, I see all the elastic jars with the correct version of 6.2.2 -- EXCEPT for transport. That jar is not there at all. I looked in my executable jar, and it's definitely not there. Shockingly, my app throws a class not found error for org/elasticsearch/common/transport/InetSocketTransportAddress
How do I get that jar into my dependencies?
EDIT
So I added:
<properties>
<elasticsearch.version>6.2.2</elasticsearch.version>
</properties>
to my pom, and overrode the spring version, but the transport jar is not showing up in my dependency tree! How do I get this file into my executable jar?

mssql-jdbc version 6.2.2.jre8 dependency results in version 6.1.0.jre7 in war

I have the following dependency in my Maven project
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.2.2.jre8</version>
<!-- Exclude unused Azure dependencies -->
<exclusions>
<exclusion>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-keyvault</artifactId>
</exclusion>
<exclusion>
<groupId>com.microsoft.azure</groupId>
<artifactId>adal4j</artifactId>
</exclusion>
</exclusions>
</dependency>
I am using the newest non-preview mssql-jdbc release in Maven here
https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc
However when I run mvn clean package my resulting war file has mssql-jdbc-6.1.0.jre7.jar in WEB-INF/lib instead of the expected mssql-jdbc-6.2.2.jre8.jar. I have tried clearing my local .m2 repository and repackaging but that did not help. It is worth noting that 6.1.0.jre7 is actually the oldest version out there, so my guess is that for some reason it cannot find the newest one and is reverting to the oldest? I'm stuck.
I encountered same problem, and I am using Spring Boot 1.5.x.
The root cause is: Spring Boot(1.5.x) specified the version of "6.1.0.jre7".
Solution is: specify your version in the main module which contains Spring boot Application.
Previous part of my dependencies:
Sub module 1:
<groupId>com.my.company</groupId>
<artifactId>sub-module1</artifactId>
<version>1.0</version>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.1.3.jre8-preview</version>
</dependency>
Main module, which contains Spring boot Application:
<dependency>
<groupId>com.my.company</groupId>
<artifactId>sub-module1</artifactId>
<version>1.0</version>
</dependency>
And I found Main module boot.jar included "mssql-jdbc-6.1.0.jre7.jar".
Finally I found the version is specified in "spring-boot-dependencies" which is the parent of "spring-boot-parent".
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.16.RELEASE</version>
<mssql-jdbc.version>6.1.0.jre7</mssql-jdbc.version>
Solution:
Specify the version in main module:
<dependency>
<groupId>com.my.company</groupId>
<artifactId>sub-module1</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.1.3.jre8-preview</version>
</dependency>
Checked the result by mvn denpendency:tree, it's ok

java.lang.NoClassDefFoundError: org/apache/http/conn/SchemePortResolver with AmazonHttpClient

All
I am running into this error in my project when I updated aws library to the latest 1.11.3.
Caused by:
java.lang.NoClassDefFoundError: org/apache/http/conn/SchemePortResolver
at com.amazonaws.http.apache.client.impl.ApacheHttpClientFactory.<init>(ApacheHttpClientFactory.java:40)
at com.amazonaws.http.AmazonHttpClient.<clinit>(AmazonHttpClient.java:97)
at com.amazonaws.AmazonWebServiceClient.<init>(AmazonWebServiceClient.java:145)
at com.amazonaws.services.s3.AmazonS3Client.<init>(AmazonS3Client.java:393)
at com.amazonaws.services.s3.AmazonS3Client.<init>(AmazonS3Client.java:373)
at com.amazonaws.services.s3.AmazonS3Client.<init>(AmazonS3Client.java:355)
at com.amazonaws.services.s3.AmazonS3Client.<init>(AmazonS3Client.java:339)
in my pom.xml
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<version>1.11.3</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.3</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-kms</artifactId>
<version>1.11.3</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-ext-jdk15on</artifactId>
<version>1.54</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-encryption-sdk-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Anyone know what I did wrong?
thanks
I had a similar issue with my grails application. In my case the ClassNotFoundException was being thrown from a deploy script. For me the reason SchemePortResolver wasn't being resolved implicitly was because it wasn't required at compile time, it was needed at runtime. Here's what I added to my BuildConfig.groovy to fix it:
runtime 'org.apache.httpcomponents:httpclient:4.5.2' //Required by BeanstalkDeploy.groovy at runtime
Since the OP's question was for Maven, here's the equivalent include:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
<scope>runtime</scope>
</dependency>
If you add,
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
,it should work ok as it contains the missing class definitions.
A common cause would be a Maven dependency conflict. In the example below (pom.xml as viewed in Eclipse's Dependency Hierarchy tab), the POM explicitly includes v4.1 of httpclient, which forces Maven to omit the v4.5.5 that aws-java-sdk-core requires (and thus, causes the similar error java.lang.NoClassDefFoundError: org/apache/http/conn/DnsResolver at com.amazonaws.http.apache.client.impl.ApacheHttpClientFactory...):
In my case I delete the .meta file in eclipse workplace then import the project again thereafter it work like a charm.
Could not know where the problem exactly.
Before delete .meta I add all files from
aws-java-sdk-1.11.606\third-party\lib

Eclipse Maven Dependency issue

I have maven-depenencies folder which lists over 50+ jars I need for compile and testing on my local. In addition POM.xml have specific (see snippet) which lists the dependencies I wanted in "target/final_build.jar". I do not want rest of maven - dependencies I can see on eclipse IE. I just want following packaged as aprt of final jar..
What is the easy way to accomplish . I tried copy-dependencies but it copied all Maven dependencies and not the 4 listed in pom.xml. More over they are copied over to lib/src folder.
Desired state is to just have 4 dependencies mentioned below are part of "target/outputfile.jar"
<dependencies>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-stream</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
Dependencies that are needed for compile/test but not for the application (because the (EE-/JDK-/?-) Container already have this classes) can be specified by the dependency scope provided:
<dependencies>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>3.0.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-stream</artifactId>
<version>3.0.3</version>
<scope>provided</scope>
</dependency>
</dependencies>

NoClassDefFoundError. How to set dependencies and deploy a java app?

My Java app requires org.objectweb.asm library. I specified 'asm' dependency in pom. That deploys the library together with the app. Still the app throws exception NoClassDefFoundError: org/objectweb/asm/ClassVisitor.
java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor
at com.sun.jersey.api.core.PackagesResourceConfig.init(PackagesResourceConfig.java:112)
at com.sun.jersey.api.core.PackagesResourceConfig.<init>(PackagesResourceConfig.java:78)
... many more
How can I fix the problem?
Details:
I am using Glassfish 2.1.1. The app requires jersey 1.1.4, jersey requires asm 3.1. I assume 1.1.4 version is required by glassfish 2.1.1
If I run Glassfish updatetool and install Jersey on the server then my app loads and runs with no problems. My client doesn't have Jersey installed on their server and they can not use updatetool.
Glassfish 2.1.1 updatetool installs jersey 1.1.4 and asm-3.1.jar in glassfish/lib directory.
When jersey is uninstalled, updatetool removes asm too.
If I include jersey and asm as dependencies and deploy my war file then jersey and asm jars go into local location, e.g. glassfish/domains/domain1/applications/j2ee-modules/MYAPPNAME/WEB-INF/lib/asm-3.1.jar.
Glassfish updatetool puts asm into lib folder: glassfish/lib directory the app start deploying and working correctly.
Here is my maven pom file dependency section:
<dependencies>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-grizzly2</artifactId>
<version>1.17.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-6.0</artifactId>
<version>1.0.0.Final</version>
<scope>provided</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
I had a conflict with an older jersey library stored in glassfish/lib folder:
glassfish/lib/jersey-bundle-1.0.3.1.jar
It was loaded and used instead of jars placed in a folder local to the app:
glassfish/domains/domain1/applications/j2ee-modules/MYAPPNAME/WEB-INF/lib/
Jersey jar from 'lib' folder was looking for 'asm' library in the same 'lib' folder, hence my locally placed asm-3.1.jar was never found.
I expected that 'local' jars were loaded first. Appeared that the search order is different. Is that really the case? Please tell me if I am wrong.
I found the problem by verifying which library is used with the following code:
logger.debug(PackagesResourceConfig.class.getResource("PackagesResourceConfig.class"));
Note PackagesResourceConfig class listed in the exception thrown.
Hopefully this answer will help someone else to save their time.
Try something like that in your pom:
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>

Categories