Java webservice NoClassDefFoundError - java

I am generating a webservice stubusing this statement in java
new TPFServiceStub(webserviceUrl);
I have created a mock service in soap UI at 8088.
The same URL I am passing in the webserviceUrl variable.
All the dependent jars are placed in axis_home.
I am getting this following error.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/axiom/om/OMDataSource
at com.arcot.csso.credchangereportsvc.dao.CIWebserviceDAO.getServiceObject
Can you please help me out :)

You have the axiom jar containing the OMDataSource class in your deployment classpath?

I'm guessing the library/framework you've been using to implement your WebService endpoints uses Apache Axiom as SOAP Message Factory. So, you should make sure you have Axiom jar on your classpatch.
You can get the latest binary for Apache Axiom here: http://ws.apache.org/axiom/download.cgi
In case you've been yet another happy user of Maven you can simpply add the following dependency to your pom.xml file:
<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom</artifactId>
<version>1.2.11</version>
<type>pom</type>
<scope>runtime</scope>
</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

Error while creating spring MockHttpServletResponse, MissingResourceException even after adding javax.servet-api dependency

I am writing test for a servlet filter using spring MockHttpServletResponse and request. I am getting the following error while creating MockHttpServletResponse object.
Caused by: java.util.MissingResourceException: Can't find bundle for base name javax.servlet.LocalStrings, locale en_US
I tried couple of things suggested on stackoverflow like adding javax.servlet-api to pom but even with that its not working.
I tried adding tomcat server api in pom, but that is also not working.
My pom file just contains servlet api, jmockit and spring-test dependencies. And I dont have any message.properties or any other resource in classpath.
Is there any other dependency that I need to add here?
You need the appropriate version of javax.servlet:javax.servlet-api in the test classpath.
For Maven, you will need something similar to the following.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>

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

Apache CXF Dynamic Client creation

I am trying to use Apache CXF to talk to a unknown web service. I have followed the Dynamic Client example from Apache.
JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
Client client = factory.createClient(wsdlURL.toExternalForm(), SERVICE_NAME);
This was working but now i am getting this exception when calling createClient():
java.lang.IllegalStateException: Unable to create schema compiler
Caused by:
javax.xml.bind.JAXBException
- with linked exception:
[java.lang.ClassNotFoundException: com/sun/tools/internal/xjc/api/XJC]
This looks similar to an existing bug. I am using DOSGi singlebundle 1.2 which includes cxf-minimal-2.2.9.jar; meaning the bug should be fixed in the version I'm using. the jaxb-api is included in my Apache CXF distribution which upon inspection contains jaxb-xjc.
Can anybody provide me some insight as to what I'm doing wrong? I swear this used to work.
"java.lang.ClassNotFoundException: com/sun/tools/ " is often occurs if you use JRE in your IDE intead of JDK.
Make sure, you use JDK in IDE (e.g. eclipse)
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>2.2.11</version>
</dependency>
resolved problem
Another solution is to include the cxf-rt-core in your Maven dependencies.

JAXWS code generation and implementation along side Spring for JDK1.5

We're trying to generate source code stubs from a series of WSDLs (each with their own XSDs). We can do this fine and works perfectly under JDK1.6, however we need it to work under JDK1.5.
We're using jaxws-maven-plugin to generate the source code, however it depends on the wsimport binary being available (this is not available in JDK1.5). We found a work around for this, we can now generate the source code.
The final problem comes when we try and use the compiled stub code with Spring (2.5.6) we're using a JaxWsPortProxyFactoryBean to interface with the generated client code. We get a java.lang.NoClassDefFoundError: javax/xml/ws/soap/Addressing. I investigated why this was and found that the javax.xml.ws/javax-api/2.1 dependency did not contain this class. I upgraded the version number to 2.1-1 and now when we build the project (to generate the source code) we get the following error:
cannot find symbol
symbol : method partName()
location : #interface javax.jws.WebParam
Can anyone provide a solution to this so we can generate our JAXWS client stub code and make it work with the JaxWsPortProxyFactoryBean?
Thanks in advance
Jonathan
It seems that they mess with this jar, see this link.
The workaround is the following :
delete M2_REPO/javax/xml/ws/jaxws-api/2.1/ directory
use this dependency
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.1</version>
</dependency>
put these repositories at the top of your list :
<repository>
<id>javanet</id>
<url>http://download.java.net/maven/2/</url>
</repository>
<repository>
<id>javanet legacy</id>
<url>http://download.java.net/maven/1/</url>
<layout>legacy</layout>
</repository>
I managed to solve this one, after hours of studying the dependencies and looking at what each of them had inside I discovered that the dependency javax.xml.ws:jaxws-api:2.1-1 was required, but we had to take a copy of this dependency and take out:
<dependency>
<groupId>javax.jws</groupId>
<artifactId>jsr181</artifactId>
<version>1.0</version>
</dependency>
Then I had to include the following dependency in the pom.xml of my application:
<dependency>
<groupId>javax.jws</groupId>
<artifactId>jsr181-api</artifactId>
<version>1.0-MR1</version>
</dependency>
This is far from ideal, we have to maintain a separate proprietary dependency in our maven repository and remember to include this dependency. The annoying thing is Maven doesn't allow me to exclude by version number (only by groupId and artifactId). If anyone has a suggestion to make this solution better please let me know.
Thanks for your help.
We're using jaxws-maven-plugin to generate the source code, however it depends on the wsimport binary being available (this is not available in JDK1.5). We found a work around for this, we can now generate the source code.
Actually, wsimport, which is part of JAX-WS, is not included in Java 5 (unlike Java 6 which includes JAX-WS 2.x, Java 6u14 includes JAX-WS 2.1.6) but it is available for Java 5 as long as you provide it. The odd part is that the jaxws-maven-plugin declare these dependencies (see for example jaxws-maven-plugin-1.12.pom), there must be a classloading issue somewhere, hence the "work around".
The final problem comes when we try and use the compiled stub code with Spring (2.5.6) we're using a JaxWsPortProxyFactoryBean to interface with the generated client code. We get a java.lang.NoClassDefFoundError: javax/xml/ws/soap/Addressing. [...]
What version of the plugin are you using exactly? I'd suggest to use the version 1.12 and the same version of jax-ws as in the plugin in your pom.xml:
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-tools</artifactId>
<version>2.1.7</version>
</dependency>

Categories

Resources