NoClassDefFoundError problems with help of Apache Commons Validator - java

Getting an error if using Apache Commons Validator:
java.lang.NoClassDefFoundError: org/apache/oro/text/perl/Perl5Util
Even if, Apache Commons Validator /dependencies state that ORO.jar is optional.

I was having this issue on version 1.3.1 of Apache's Validator. I rolled back to 1.3.0 and it included the oro dependency just fine.
If you don't rely on something from the dot release, perhaps this could work for you as well.

Put the jar containing "org/apache/oro/text/perl/Perl5Util" class on the server and modify the classpath of the server to include the jar file.

The newer version (1.4.0) of commons-validator has no dependency on oro anymore.
Using Maven:
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.4.0</version>
</dependency>

Apparently you're using something that requires that class.
Compiler wins over documentation.

You need to get Apache oro. I've version 2.0.8 though Maven and it has Perl5Util:
mpandit-mbp:2.0.8 mpandit$ jar tvf oro-2.0.8.jar | grep 'Perl5Util'
7818 Sun Dec 28 23:00:18 PST 2003 org/apache/oro/text/perl/Perl5Util.class

You can download this dependency to get Perl5Util.
<dependency>
<groupId>oro</groupId>
<artifactId>oro</artifactId>
<version>2.0.8</version>
</dependency>

Related

Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.Transformer [duplicate]

I am receiving the following error java.lang.NoClassDefFoundError: org/apache/commons/collections/Transformer trying to use BeanMap from the Apache Commons BeanUtils library.
It is generated from the following code: BeanMap studentBeanMap = new BeanMap(cohortStudentData.get(row)); where cohortStudentData is a list of beans.
I am using BeanListHandler from Apache DBUtils to form the list of beans from a database.
I understand from this and this bug report that BeanMap is dependant on the Apache Collections framework. However, I have imported all relevant libraries into my project and into my class, as you can see below:
Does anyone know why this might be happening?
I am not really sure, but i think your error is because of jar versions. Lately apache has changed the package of the new versions of their jars because they implement new functionality or something that is not fully backward compatible. For example the jar commons-beanutils-1.9.2.jar depends on commons-collections-3.2.1.jar (according to this site) and you are using commons-collections-4.4.0.jar. If you are planning using the universe of apache jars, you need to be sure that they are all compatible.
Just add this dependency to your project.
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
commons-collections4-x.x.jar Add the library to your classpath and try to run again. It will work.
Download the library from:
https://mvnrepository.com/artifact/org.apache.commons/commons-collections4/4.1
Adding dependency of version 3.2.1 seems working here
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
Add commons-collections-3.2.jar to library of the project

Upgrading CXF and WSS4J but can't find dependencies

So I have a working project that I am trying to upgrade CXF and WSS4J
It looks like I have upgraded my client ok, but that's because I am using this for wss4j:
<dependency>
<groupId>org.apache.wss4j</groupId>
<artifactId>wss4j</artifactId>
<version>2.1.8</version>
<type>pom</type>
</dependency>
But my server is throwing ClassNotFound exceptions, obviously because of the type=pom.
So where do I find the new classes jars? Specifically WSPasswordCallback.
BTW, I am using CXF 3.1.9
The short answer is that they moved everything. The first place you want to look is: https://mvnrepository.com/artifact/org.apache.wss4j
That is where all the new ones are. From there you will simply need to google the class you want and see if you can find the package you need. For me WSPasswordCallback moved to the commons package...
<dependency>
<groupId>org.apache.wss4j</groupId>
<artifactId>wss4j-ws-security-common</artifactId>
<version>2.1.8</version>
</dependency>
IMHO this could have been handled better, but it's a start to find what you need.

java.lang.NoSuchFieldError: INSTANCE in HttpClient when running Oozie

I have a Java app (as a Maven project) that uses HttpClient (v 4.5) in accessing a REST API, and then writes the GET response in HDFS as a JSON.
This works fine when in Eclipse IDE. Here's my dependencies:
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.2.0</version>
</dependency>
When I try to integrate it with Oozie, because I'm planning to make the REST API call and HDFS write periodic, I encounter:
Caused by: java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:144)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:955)
at com.accenture.pdc.digital.sf.datascience.RestController.<init> RestController.java:26)
at com.accenture.pdc.digital.sf.datascience.App.main(App.java:53)
(RestController.java and App.java are my classes).
When I trace, the problem lies in this line on my code:
CloseableHttpClient httpClient = HttpClients.createDefault();
and on further probing in the libraries source code, there's this deprecated:
AllowAllHostnameVerifier.INSTANCE
When I searched about this same problem, some said it could be a multiple version conflict of HttpClient or HttpCore. So the possible culprit could be the HttpClient dependency of hadoop-client (just my hunch). How can I tell if it is a multiple version conflict? If it is, how do I resolve it?
If not, are there alternative solution in opening a HttpClient to avoid this issue? Should I use library other than HttpClient?
The first alternative that worked for me is to use Jersey instead of HttpClient. Since hadoop-client already uses jersey-core and jersey-client as a dependency, might as well use what's existing already. Also, I upgraded hadoop-client to 2.6.0, since the Hadoop version I have is 2.6.0.
Then when I removed the http-client in pom.xml, I've noticed that http-client, version 4.2.5, suddenly appears. It confirms that hadoop-client also have it as dependency, as I suspected earlier. So when I reverted to HttpClient solution, and adjusted some lines to suit the older version, it runs successfully in Oozie.

java.lang.NoClassDefFoundError: org/apache/commons/collections/Transformer

I am receiving the following error java.lang.NoClassDefFoundError: org/apache/commons/collections/Transformer trying to use BeanMap from the Apache Commons BeanUtils library.
It is generated from the following code: BeanMap studentBeanMap = new BeanMap(cohortStudentData.get(row)); where cohortStudentData is a list of beans.
I am using BeanListHandler from Apache DBUtils to form the list of beans from a database.
I understand from this and this bug report that BeanMap is dependant on the Apache Collections framework. However, I have imported all relevant libraries into my project and into my class, as you can see below:
Does anyone know why this might be happening?
I am not really sure, but i think your error is because of jar versions. Lately apache has changed the package of the new versions of their jars because they implement new functionality or something that is not fully backward compatible. For example the jar commons-beanutils-1.9.2.jar depends on commons-collections-3.2.1.jar (according to this site) and you are using commons-collections-4.4.0.jar. If you are planning using the universe of apache jars, you need to be sure that they are all compatible.
Just add this dependency to your project.
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
commons-collections4-x.x.jar Add the library to your classpath and try to run again. It will work.
Download the library from:
https://mvnrepository.com/artifact/org.apache.commons/commons-collections4/4.1
Adding dependency of version 3.2.1 seems working here
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
Add commons-collections-3.2.jar to library of the project

java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INSTANCE from Mashape Unirest in Java application

I have a Maven Java project that uses Mashape Unirest for sending HTTP requests to other URLs. I am currently writing an integration test (using TestNG) that sends a normal HTTP request using Unirest. When I run the integration test through Maven (via the Failsafe plugin), the request is sent out successfully. However, when I try to run the integration test via Eclipse, I keep on getting the following error:
FAILED: getCurrentTimeTest
java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:52)
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:56)
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<clinit>(DefaultHttpRequestWriterFactory.java:46)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:72)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:84)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<clinit>(ManagedHttpClientConnectionFactory.java:59)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$InternalConnectionFactory.<init>(PoolingHttpClientConnectionManager.java:487)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:147)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:136)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:112)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:726)
at com.mashape.unirest.http.options.Options.refresh(Options.java:41)
at com.mashape.unirest.http.options.Options.<clinit>(Options.java:27)
at com.mashape.unirest.http.HttpClientHelper.prepareRequest(HttpClientHelper.java:141)
at com.mashape.unirest.http.HttpClientHelper.requestAsync(HttpClientHelper.java:80)
at com.mashape.unirest.request.BaseRequest.asStringAsync(BaseRequest.java:56)
at ...
I am also able to reproduce this error using a basic Java application script.
I have made sure that the dependencies I am using in my pom.xml file are the latest and greatest, as seen below:
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.2</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3.2</version>
</dependency>
I have also checked out the source code of BasicLineFormatter.java, both from the source file downloaded to Eclipse and from Apache's Httpcore Github repo. In the Github repo, notice how the INSTANCE field is defined for the 4.3.x branch and the trunk branch, but not in older branches like 4.2.x. However, I am indeed using version 4.3.2 in my project, so I should be using a JAR file for Httpcore that has the latest version of BasicLineFormatter. I know that, based on the Maven Dependencies JAR files that are in my project, that I am indeed using the latest versions of these Apache dependencies, not the older versions specified as downstream dependencies of my project.
I have checked other various SOF and blog posts about this issue, such as Mashape Unirest Java : java.lang.NoClassDefFoundError and this blog post too, but they all seem to be talking about solving the NoSuchFieldError problem for Android. However, I'm dealing with a standalone Java application, not an Android application.
I am at a loss in determining how to troubleshoot this issue. Anyone have any idea what I need to do?
UPDATE
Instead of showing my test case, I will reduce the illustration of a reproduction of this problem to just a simple one-liner Java application, because the problem exists with any Java application or test case run through Eclipse, not just one particular test:
System.out.println(Unirest.get("http://www.google.com").asStringAsync().get().getBody());
Normally, this should print the HTML of the Google home page, but I instead get the NoSuchFieldError stack trace.
FIXED!
The problem was that the AWS SDK (it's on my classpath because I'm developing for Elastic Beanstalk) had a conflicting JAR file. Using Oleg's solution (thanks BTW), I printed the following output in a unit test:
jar:file:/some/path/aws-java-sdk/1.7.1/third-party/httpcomponents-client-4.2.3/httpcore-4.2.jar!/org/apache/http/message/BasicLineFormatter.class
I'll have to rearrange my classpath so that AWS SDK is no longer conflicting.
The only plausible explanation to this problem is there is an older version of HttpCore on the classpath (unless you also want to consider a possibility of green men from Mars messing with your computer remotely from a flying saucer).
You can add this snippet to your code to find out what jar the class gets picked up from. This might help find out why that jar is on your classpath in the first place.
ClassLoader classLoader = MyClass.class.getClassLoader();
URL resource = classLoader.getResource("org/apache/http/message/BasicLineFormatter.class");
System.out.println(resource);
This basically tells me that in my case the jar resides in the local maven repository and likely to have been added to the classpath by Maven
jar:file:/home/oleg/.m2/repository/org/apache/httpcomponents/httpcore/4.3.1/httpcore-4.3.1.jar!/org/apache/http/message/BasicLineFormatter.class
As already mentioned by previous comments, It's mainly because of the conflicting versions of httpcore jar, the static field INSTANCE is been added to BasicLineFormatter class in versions > 4.3.1, Though you might have added the latest version of the httpcore jar in your dependencies, but its highly possible that other (lower) version of jar is getting picked up.
So, first to confirm that, wrong jar is getting picked up, Use the following line of code -
ClassLoader classLoader = <Your Class>.class.getClassLoader();
URL resource = classLoader.getResource("org/apache/http/message/BasicLineFormatter.class");
System.out.println(resource);
If this prints, the lower version of the jar, then it's confirmed that it's picking the lower version of the httpcore jar (May be from other dependencies of your project),
Solution -
Add following maven/gradle dependencies at the top of dependency list (Or above the other project dependency which caused the conflict) -
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.4.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.4.1</version>
</dependency>
I faced the same exception using unirest:
java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:52)
at com.mashape.unirest.http.options.Options.refresh(Options.java:55)
at com.mashape.unirest.http.options.Options.<clinit>(Options.java:36)
And found it was due to DefaultConnectionKeepAliveStrategy.INSTANCE; and the conflicting jar was apache-httpcomponents-httpclient.jar in my classpath. Adding this post to help anyone who faces similar exception
I got this Exception: Caused by: java.lang.NoSuchFieldError: INSTANCE
Solution:
This happens if you have two different version classes in your classpath…. […], So I first find that class (one version of class), click that class, select build path, then I click remove from build path.
if you are using aws sdk this error occurs because of dependency mismatch.
To avoid this error do the following:
1.Put the dependecies in the required order aws sdk and the end preferably
2.Add shade plugin to the project
This solved my problem
you can refer to my answer in
HTTPClient Example - Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
my case is i have httpclient-4.4.1.jar, and httpcore-4.4.1.jar in my class path, but JVM loaded BasicLineFormatter from httpcore-4.0.jar

Categories

Resources