How do I make tomcat include libs from the current project? - java

I've read somewhere as a good practice, that it's better to have the required libs included in each project, then just add all the libs you'll ever need to the tomcat folder. Well right now Tomcat seems to need all the required libs to be in lib folder of Tomcat. How can I make it use the libs from the build path of the project? i'm using tomcat 7.032 in through Eclipse.

The libraries of a webapp must be in the WEB-INF/lib directory of the deployed web application directory or war file.
Every jar file in this directory will be in the classpath of the webapp, and won't be in the classpath of the other deployed webapps. You indeed shouldn't put webapp libraries into Tomcat's classpath.

Related

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.

Library dependencies outside WAR file

I have a requirement where i need to pack all the common classes of various web services and make them as dependencies(To avoid duplicates for multiple services). The structure of my EAR file is as follows:
META-INF/MANIFEST.MF
META-INF/application.xml
commons-codec-1.2.jar
MyWebService1.war
MyWebService2.war
MyWebService1.jar
MyWebService2.jar
EJB_Bean.jar
Now i have included the dependency jar's under WAR file's Manifest as follows(Not sure if appropriate),
`
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.4
Created-By: 1.7.0_51-b13 (Oracle Corporation)
Class-Path: MyWebService1.jar
In the stated structure when i try to deploy the EAR file onto web-logic server, i get java.lang.NoClassDefFoundError, But whereas if i move the dependency jars under WEB-INF/lib folder of my WAR and repack my EAR file, Deployment goes fine as expected.
Is there any way i can include my WAR file dependency libraries outside WAR file or even outside my EAR file is my question. Any help or suggestion would be appreciated.
A simple way to set the libraries of an EAR package is to create a lib folder right under the the EAR root and put your libraries inside.
In this case you will have something like this in your EAR:
META-INF/MANIFEST.MF
META-INF/application.xml
MyWebService1.war
MyWebService2.war
lib/commons-codec-1.2.jar
lib/MyWebService1.jar
lib/MyWebService2.jar
EJB_Bean.jar
In tomcat jar files can be placed inside the lib directory and these jar files gets available to all the applications deployed on that server. In web-logic there must be the similar way to do it. A lib folder from where the web-logic server loads all the jar files.
My first advice would be to use some kind of dependency management such as Maven, Gradle or Ivy - doing that by yourself is very difficult and error prone.
A simpler solution in your case would be to take your "common jars" (especially if they are shared between multiple applications), and add them to the shared library of your webserver. As an example, Tomcat will load all librairies that are placed under tomcat-dir/common/lib directory.

How to add jars available in a particular folder to classpath at runtime only for specific wars:

I am using maven build tool.
The following two are my intentions.
1) To move some of the third party library jars out of my war from WEB-INF/lib folder [note: These jars are common between more than 2 war files (or artifacts)]
2) To make the war file small in size.
Is it possible to move those jars out of war and put it into a folder and these jars should be referred in the classpath only by the wars which require it.
I have tried adding the path to the jars in the Class-Path: of MANIFEST.MF of war files but it did not work out. Please help me out.
I assume the jars in question are necessary for compiling the code that make up the web application. If the third-party jars are necessary for compilation but you don't want them in your war file, you have two options:
In the <dependency></dependency> section for the third-party jar, add "<scope>provided</scope>".
Configure the maven war plugin to exclude the jars you don't want in the war. See http://maven.apache.org/plugins/maven-war-plugin/examples/skinny-wars.html for the details.
How you get the jar files that you exclude from the war into the web container's classpath depends on your environment. If you are using ear files, the Maven docs above talk about how to get the jars included in the ear file. If you are not using ear files, you will have to come up with your own way to get the jars into the container's lib directory.

WAR loads differently on weblogic when inside an EAR, why?

How does WebLogic 11g load libraries in an EAR file? I have this problem with a web application, that when deployed as a WAR (with libraries it depends on in WEB-INF/lib), it works just fine. However, when it's inside an EAR file, WebLogic does not find those libraries unless I put them in APP-INF/lib. Does that mean that if I'm deploying as an EAR I'd have to pull out all JAR files from the WEB-INF/lib directory and place them in APP-INF/lib ? or is there a configuration that can be done in WebLogic to avoid this?
Thanks!
If you have JAR files that you need to share between multiple WAR files or between WAR files and EAR files then you will need to package them in the EAR.
If WAR#1 has a JAR in its WEB-INF/lib and is packaged in an EAR with WAR#2, then WAR#2 will not be able to see the JAR files in WAR#1/WEB-INF/lib.
Solving your problem will take some understanding of how Java EE classloading works in a container. You should look at this link to get an understanding, but the basic problem is that when you package your application as an EAR, you've introduced another classloader (the application classloader) into the class loading hierarchy. You can configure WebLogic to load from your webapp by using the prefer-web-inf-classes element.

I need JAX-WS runtime on Tomcat. Is it ok to put "jax-ws/lib/*" to "tomcat/lib"?

I need JAX-WS runtime on Tomcat.
Is it OK to put "jax-ws/lib/*" to "tomcat/lib" ?
Does one need any Axis, Axis2 libs in Tomcat if he puts jax-ws/lib into tomcat/lib?
At least, any of axis*.jar are needed for Tomcat to function properly?
As a general rule you should avoid putting jar files in the tomcat/lib folder. Tomcat doesn't need jar files other than those originally to be found in its lib directory.
If you need a jar file you should put it in WEB-INF/lib folder of your application. This will save you a lot of headaches with jars versioning.

Categories

Resources