How to add jar to Google App Engine with Eclipse? - java

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.

Related

Library is not copied to Tomcat directory when deploying

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.

Adding External JARs to Openshift

I have a pretty basic Java web application (created using Eclipse openshift plugin). The app runs fine on the OpenShift server. Now I need to use an external JAR library for JSON parsing and creation in Java. I can't understand my way around how to add that JAR to openshift server.
I went to project properties>>BuildPath>>Add external JAR. While that does import the JAR, it does not work on openshift server and gives error relating to that file's import (NoClassDefFound, I guess). Could you tell me how to import external jars into openshift project?
Since we use Maven for the build process, all you have to do is add it as a dependency in your pom.xml
Here is an example that adds a mongo driver
https://github.com/thesteve0/openshift-mongo-spatial-jee6/blob/master/pom.xml
Look under the dependencies tag
You can add the jar as a dependency in your pom.xml file. This will cause the jar to be included in your war. If the jar is not available in a public maven repo, please see here: http://www.billdecoste.net/?p=16
[Updates in 2017]
You might want to take a look at this link from OpenShift itself.
It includes information about adding jars with and without maven dependencies.

How to organize gae projects in eclipse

I try to find the way to organize a GAE with several projects within Eclipse using the Google plugin for GAE:
The Web App project (a WebApp project) containing the GAE web application.
A Java project with data access
A Java project with utility classes
My problem here is how to link things together. I want to add the two Java projects in both build and execution paths. Since a Web App project follows the JavaEE structure, only what is specified in the WEB-INF/lib directory is taken into account.
I would like to find out how to simulate a Jar file in this directory based on a Java project present in the Eclipse workspace based on what the Google Eclipse plugin for GAE provides.
I saw something that seems to be related in the WebApp project properties Google > Web Application, section "Suppress warnings about these build path entries being outside of WEB-INF/lib".
For the GAE web application to run then you'll need the classes or a jar from the projects you want to include in the WEB-INF/classes or WEB-INF/lib folders respectively.
One way would be to build your data and utility projects and put the resulting jars in the WEB-INF/lib folder. You can then then reference those jars as libraries from your web app and all should be fine. Of course that's a bit tiresome to do manually, so you should probably check out some dependency management tools. From personal experience Ivy and IvyDE were easy to get into and should cover your needs although Maven and others have their strengths.
Another way that is a easier (but less structured) is to used linked source folders in your build path (to the source folders for your data and utility projects). In such way Eclipse will build those sub projects to WEB-INF/classes and build and execution should work similarly.

GWT web-app vs system classpath for dynamic loaded classes

In my GWT web app I am keeping all my jar files outside of my project and referencing them using classpath variables. This allows me to link to jars from other projects/teams without having to put a copy of the jar in my web app lib directory. Hosted mode kindly looks up the classes in this system classpath and then adds them to the web-app classpath warning me that it is doing so. When I deploy my build system pulls in only the jars I need to ship in my web app and is not a problem.
The issue I have is that some code uses dynamic lookups, searching the classpath for implementations. If a jar has not yet been added to the web app classpath beacuse no classes have yet benn loaded from the jar it is not included in the search.
The particular problem I am having is with Persistence - it looks up EntityManagerFactory implementations by searching for META-INF/services files. I have also had a similar problem with Rome and its module extensions.
I have got a workaround, in the dev/hosted mode I simply refer to a class I know is in a jar I want and this causes it to be added to my web app classpath. I do this my by calling
private void devModeClassPathHack() {
Class<?> gwtDevModeHack1 = EntityManagerImpl.class;
}
from my development mode Guice module.
My question is simple - is there a "nicer" way of doing this?
I don't think, there's a nicer way - using this makeshift "self-healing" mechanism of the GWT Jetty/dev mode is already a hack :) If you ever have the need to run the server side code on e. g. JBoss during development (together with GWT dev mode for the client side), this will stop working.
(As an additional problem, the order of classloading - and consequently the precedence in cases when there are multiple versions of a class in different jars - will depend on program flow. This can be very nasty to debug.)
You mention, that you pull in all the jars from outside. What I would do in that case, is to set .svnignore (or .gitignore/...) to "*.jar" in the WEB-INF/lib dir. Then I'd run the same build step which is used to create the production build to copy the jars into the lib dir before starting the server.

Error ClassNotFoundException accessing jar in web project

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.

Categories