java.lang.NoSuchFieldError: REFLECTION during Tomcat deploying - java

I have an application which is able to run properly in tomcat, but when added a dependency of another application in maven I encountered the error below when deploying on Tomcat (the build works just fine):
java.lang.NoSuchFieldError: REFLECTION

There could be conflicts in the jars as imported by your application and as that are already present in the lib folder of tomcat directory.
Remove the duplicate jars from the lib folder tomcat directory. That may resolve the issue.

Teja: has maven shade helped you anyhow? I'm following a similar issue in Jetty now and I'm interested in trying different approaches. So far I've discarded different versions that may be adding noise.

Related

MyEclipse not deploying link source to WEB-INF/classes

I have a web project web and a java project common.
I set common's source folder as web's link source and used classes in common on web project without error.
But when I deploy web on tomcat, I always get NoClassDefFoundError exception. Some classes in common are not found. When I check deployed files I find there are only web's classes in WEB-INF/classes. But both common and web's classes are compiled to default output folder (web/target/classes) set in Java Build Path. If I copied all these classes to WEB-INF/classes, everything works.
I have checked Deployment Assembly setting but it's already correct. Both /common(in common) and /src(in web) are set to Deploy Path WEB-INF/classes.
I'm using MyEclipse 10 and tomcat 7.0. How can I resolve this? Thanks in advance.
Still don't know why but I switched to another plan and made common as a maven package. I imported it into web as dependency through maven. Now it works.

java.lang.ClassNotFoundException: javazoom.spi.mpeg.sampled.file.MpegAudioFileReader [duplicate]

I'm using Eclipse 3.7 (STS) with Tomcat 7 running inside the IDE. I've created a new Dynamic Web project and added a single JSP file to the web content root folder. I can run Tomcat and access the JSP from within Eclipse with no problems.
I've added a few 3rd party JAR's to the project from User Libraries (I'm not using maven or auto dependecies managment). In the JSP I reference a class from the project's JAR file, I can compile this with no problem, but when I deploy on Tomcat the JSP throws ClassNotFoundException. Clearly, Tomcat can't find the JAR's from my library settings. I tried creating a Run As configuration for Tomcat Server and I set the classpath to match the classpath settings of the project, but I still get the same classnotfound problem.
I could get around the issue by manually copying all project JARs to the WEB-INF/lib directory so the webapp can find all dependencies, but that's absurd and I don't expect that to be the solution since it's a maintenance nightmare.
Am I missing something?
In project's properties, go to Deployment Assembly. Add there the buildpath entries as well which you've manually added as user libraries. It'll end up in /WEB-INF/lib of the deployed WAR.
You'll need to copy the jar files to the WEB-INF/lib folder: that is where they are supposed to be.
Eclipse should offer you the option of generating a WAR file that includes all the dependencies: I haven't used Web Tools for a good while but one way or another all dependencies have to be in WEB-INF/lib or the class loader won't be able to find them.

Jersey Startup Issue : java.lang.NoSuchMethodError: com.google.common.collect.Sets.newIdentityHashSet()

I am using Jersey 2.5.1. I have my .war file packaged inside my .ear file. In the WEB-INF\lib folder of the .war module I have guava-14.0.1.jar. jersey-common-2.5.1.jar has dependency on guava.
Outside, in the ear/lib folder I have another guava jar --> guava-17.0 which is required by my project.
Both the jars have the method newIdentityHashSet(), but still I face the following error:
java.lang.NoSuchMethodError: com.google.common.collect.Sets.newIdentityHashSet()Ljava/util/Set;
at org.glassfish.jersey.model.internal.CommonConfig.(CommonConfig.java:220)
at org.glassfish.jersey.server.ResourceConfig$State.(ResourceConfig.java:110)
at org.glassfish.jersey.server.ResourceConfig.(ResourceConfig.java:351)
at org.glassfish.jersey.servlet.WebComponent.createResourceConfig(WebComponent.java:444)
at org.glassfish.jersey.servlet.WebComponent.(WebComponent.java:302)
Truncated. see log file for complete stacktrace
I have tried removing one of the guava jars, but the error is same. I have tried keeping same version in both the places. But didn't work.
I am deploying in Weblogic 12C.
Any help on this will be great.
It is not good idea to have duplicated jar files into the same ear/war file. Because errors of this type will appear. The best practice is to have only one version by library.
With that being said, you can check where is trying to load the class that don't have that method:
One way to check from which jar file are loaded specific classes, is setting the parameter: -verbose:class to the JRE.
This will give you a hint on this classloading issue.
Also WLS has bundled Classloader Analysis Tool (CAT) that helps you to identify duplicated jar files (conflicts, etc.)

Spring jars in tomcat lib folder

I am completely confused over how the class loading happens in tomcat. So please bear with me if my question sounds stupid.
We deploy multiple spring webapps on single tomcat server. To reduce the memory footprint, we thought of having spring, hibernate and database driver jars in tomcat lib folder so that those are shared by all webapps.
I started first by marking spring dependencies as provided and copied those jars to tomcat lib. But on server startup I started getting multiple ClassNotFoundErrors, like for commons-logging, commons-fileupload, jackson, so I had to move these jars as well to tomcat lib.
But then this started feeling fishy. What if in future I add another spring dependency to my project, say spring-data-cassandra. Would I need to move it's dependent jars as well? This might be unending. Also I might get CNF errors at runtime.
I tried to follow this link and brought back spring-context and spring-web back to application war. But it didn't work, got ClassNotFound on some class during WebApplicationIntializer initialization. I tried to understand the order of classes getting loaded in tomcat, but could not understand much.
Then I found a complete different explanation for JDBC driver loading which kinda contradicts to all other explanations and left me completely confused.
As I read more, I think it is not a right approach to move spring jars to tomcat lib, but still haven't got a good reasoning. And then why JDBC driver works? Can someone please explain? Also does classloader for each webapp creates a copy of each class?
Edit 1: I came to know that few dependencies are optional in spring jars, and will be required if are in use in my webapp. So spring-web depends on jackson libraries but is optional for my app. So I need to find out which all jars are required for my project and are also required by spring, those jars need to be moved to tomcat lib.
I will try to explain what I know.
When you deploy a WAR on your tomcat, the class loading will happen this way :
look for the class to load in your WAR classLoader
if not found move to parent (tomcat /lib folder)
What happen in your case is that spring also have a lot of dependencies, if you package it in your war, its dependencies would have been packaged as well and everything would have worked fine. But since you defined spring as provided, all its dependencies are considered provided as well, and when you put it in /lib folder, spring is accessible, but its dependencies are not.
What you need to do is put all spring dependencies and the dependencies of dependencies (etc.) in lib folder as well. Another solution is to define an intermediary WAR in your classloading hierarchy which will contains all your common libs.
If you prefer to create this type of thin WAR, one solution would be to first gather all runtime/provided dependencies and then copy them explicitly under Tomcat's common (or shared) library directory:
If under a Maven project, gather everything (including transitive dependencies) under target/dependency:
mvn dependency:copy-dependencies -DincludeScope=runtime
mvn dependency:copy-dependencies -DincludeScope=provided
Afterwards, copy to library. For example:
cp target/dependency/*.jar /usr/local/tomcat/lib/

Eclipse Tomcat Publishing Error: FlatFile Cannot be cast to IFlatFolder

I have an application which I can't deploy or run in Tomcat because as soon as I attempt to start Tomcat in Eclipse (with this application configured in Add/Remove Project), I get this error:
Could not publish to the server.
org.eclipse.wst.common.componentcore.internal.flat.FlatFile cannot be cast to org.eclipse.wst.common.componentcore.internal.flat.IFlatFolder
This happens on starting, publishing, and cleaning the Tomcat server in Eclipse, as long as my application is configured there. When I remove the application, I can publish fine.
What is this error about? I couldn't find any resources in Google.
Check for 'org.eclipse.wst.common.component' in your '.settings' folder in your maven project location. Check that the dependencies in that file are configured correctly. Make sure that a file (jar) is not added as a dependency at a placeholder for a folder.
I faced this issue when I added jar dependencies which were sibling modules to my project. I removed the 'dependency' tags and let maven configure the deployment descriptor for me automatically.
This worked for me and I cannot seem to find any other resource on google for this.

Categories

Resources