I am developing a JSP application to test calling a servlet with AJAX that returns a JSON object. I am using Eclipse as IDE. As library for processing JSON I am using Gson. To make that library available in my project I created a lib folder inside my source directory and copied the Gson library into it. After reloading the project in Eclipse I included it in my build path. I can use that library in my classes with no problem.
I configured the JSP application to be deployed to my local Tomcat installation. When running the application everything gets deployed to .../tomcat/8.0.26/libexec/wtpwebapps/AjaxJson so that part is working. As I understood it the directory WEB-INF contains all the classes and libraries. The classes directory contains all the classes I defined, but the lib directory is empty. Thats the reason why everytime a servlet is requested that uses the Gson library I get a java.lang.ClassNotFoundException. What do I have to do to get Eclipse to copy libraries into the lib directory when deploying the application?
Check the mappings in web deployment assembly in project properties. If you use maven, there is no need to copy dependencies manually.
It's been a long time since I used Eclipse for building WARs, but I think that marking the library as exported in the project Build Properties dialog will do the trick. If not, please do downvote.
Related
BackGround:
I am using Eclipse Lunar along with the Google App Engine plugin to try and build a website. Everything was going well until I wanted to save some data so I followed the tutorial on using Objectify. I downloaded the Objectify jar and placed it in "/war/WEB-INF/lib/" and then added it to the classpath. I then made a ObyHelper.java class as instructed in the tutorial and made a couple of my own test classes (which for now you can assume to contain no errors). I can successfully deploy too.
The question
When attempting to load the jsp page that uses Objectify (or any servlet/jsp url for that matter) I get the following error:
Uncaught exception from servlet
java.lang.NoClassDefFoundError: Could not initialize class com.googlecode.objectify.ObjectifyService
What did I do wrong and how should I have added the Objectify jar to the class path?
In order to get the jars to work in the GAE plugin for eclipse project structure you need to
1) place the jar on the WEB-INF/lib folder (in order for them to be deployed to the cloud)
2) add those jars to the classpath manually in order for eclipse to recognize them on you local dev environment.
Have you read the Setup documentation?
https://github.com/objectify/objectify/wiki/Setup
You need the guava jar as well. But really you should use Maven (or Ivy, or Gradle); manually copying jars is very old skool.
I have a very "basic" question regarding Java web application development (servlet). What is the difference between putting a jar file in WEB-INF/lib and putting it in Java Build Path/Library/External Jar?
Java Build Path/Library/External Jar is only managed by eclipse so if you build war and if your builder (or if you build externally) doesn't copy over these jars to WEB-INF/lib then you would loose them at runtime
also when you share that code with other developers, or if you use other IDE it will not help
better to use some standard and mature dependency manager / build tool like maven than copying over library to WEB-INF/lib manually
I have created a shared library with some jar files in weblogic server, now I have an EAR application and I want to use the class files from shared library in my web applications present in EAR application. So I have created weblogic-application.xml file inside my ear-app/META-INF/ directory and gave reference to shared library using below tags:
<library-ref>
<library-name>ebs_endeca_artifacts</library-name>
</library-ref>
But when I am trying to access the class files in my application, I am getting exception java.lang.ClassNotFoundException
Also it is required for me to refer to the shared library at ear level instead of declaring in weblogic.xml file of individual war files.
If I directly place the same jar files in my ear-app/APP-INF/lib directory instead of using shared library then my application is working fine without any issues.
Please help me how can I use the shared library in my ear application. I am using weblogic server 10.3.6
My shared library structure will be like this:
shared-web-app/WEB-INF/web.xml
shared-web-app/WEB-INF/lib/*.jar
You need the jars from the shared library in your classpath, because before you deploy the EAR, the application needs to be built. JDeveloper wouldn't know where are those classes stored, so that it need those at compile-time.
When preparing the application for deployment, you can create filters for the files that can be added within the WAR/EAR/<whatever>. Go to Application Properties -> Deployment and select your deployment profile. Click on the Edit button and a screen will be prompted, from where you can filter the files/libraries that can be included when the application is built.
If you don't include the shared library in the archive and have the fragment from the weblogic-application.xml you've posted, the WebLogic server will then use the classes from the shared library.
Hope that helps. Ask anything you don't understand as a comment. :)
I am using eclipse and added tomcat 6 server. whenever i try to start the server it automatically copies the server api jar into the WEBINF/lib folder and the app does not start
INFO: validateJarFile(/media/01CB9CAC704E03A0/Projects/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/myapp/WEB-INF/lib/servlet-api-2.5.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
I got the same problem and finally got it to work after looking at this among other pages. I am going to list it here in case other people run into the same issue.
In my case, the myeclipse project was created from a maven .pom file which lists the JEE 1.5 files as "provided" scope i.e. these files will be provided by the container (in my case tomcat 6). So if I create a war file using maven and drop it in the webapps dir it just works fine. It gets a little tedious doing that every time you make a change, so I decided to go for exploded deployment and thats when I ran into the same issue.
Since the project file was created from the pom file, these dependencies came in but Eclipse project file does not recognise these as "provided" and not to be deployed. And TOMCAT is not happy when it finds two implementations of the spec:
"Servlet spec 2.3 sec 9.7.2 recommends … The classloader that a container uses to load a servlet in a WAR must allow the developer to load any resources contained in library JARs within the WAR following normal J2SE semantics using getResource. It must not allow the WAR to override J2SE or Java servlet API classes. It is further recommended that the loader not allow servlets in the WAR access to the web container’s implementation classes. It is recommended also that the application class loader be implemented so that classes and resources packaged within the WAR are loaded in preference to classes and resources residing in container-wide library JARs."
I toyed with the above solution to change my deplyment assembly config but that is not helpful alone since these jars get bundled in if I select the "JARS from the build path"
in the menu project properties/MyEclipse/Web/Deplyment/Configure workspace settings.
And I need to do that cos I have other dependencies jars (not provided by container).
Solution:
I removed these jars from the dependencies in the build path manually.
Instead I added a dependency on User Library - JAVA EE5.
And in the deployment configuration I removed the option of JARS exported from User Libraries of required projects.
YMMV. But in any case if you read the servlet spec definition above and then look at the config in your workspace, you can sort it out.
Hope this helps.
Go to your project properties and look under Deployment Assembly page. This page describes how your app will be packaged for deployment or export. Take a look at entries regarding libraries. You have to figure out which of those entries points to the servlet api jar and remove it. If the entry points to other jars that do need to be packaged, you will need to split it into several separate build path entries, so you can tell Eclipse exactly what does and does not need to be packaged.
Summary
Classes from 3rd party jar files are accessible when added to build path while running a standalone JUnit test, but "ClassNotFoundException" when accessed through plain old java objects from within a dynamic web project.
IDE used: Eclipse
Web Server: Apache 7
Details
My project required a lot of 3rd party jars, namely sqlite, eclipse jdt & jsoup. I had created this project as a standalone project and all the classes from the jars were accessible fine.
But now I have copied the entire "src" folder into a "dynamic web project". I have created a folder "jars" which contains all these 3rd party jars and ensure that all of these are added to the build path by following these steps:
Project properties --> Java Build Path --> Libraries --> Add jars --> Select all the jar files from jars folder.
These jars are accessible fine when I run a standalone JUnit test inside the web project. Note that this junit test does not require a server by any means.
But when I try to "Run on Server.." , I keep on getting ClassNotFoundException like these for all the 3rd party classes:
java.lang.ClassNotFoundException: org.sqlite.JDBC
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1672)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1517)
I'm sure this has got to do something with my lack of knowledge of how applications are deployed on the web.
The runtime classpath can be different from the build classpath. Create an explicit launch configuration. The default will start with the build classpath but you may have to manually tweak it to include those 3rd party jars.
I've run into this problem before with Eclipse and the Web Server Tools project. Check your .settings files and other files for configuring the plugins that you're using with your project. You may have to remove some filters in the Eclipse view. WST constructs its build path differently, so it may not be using the jars that you've included in your project.
I figured it out. I just had to put all the jars in the WEB-INF/lib directory. I assumed that Eclipse would do all the required settings for me when I asked it to use the mentioned jars. But I guess there are somethings that are not automated very well.