Adding External JARs to Openshift - java

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.

Related

java.lang.NoClassDefFoundError: com.google.maps.GeoApiContext$Builder

I'm using the Java library for the Google Maps API but cannot find the code at runtime. I've added the repository and dependencies to build.gradle as detailed in the readme. Everything compiles fine, the import statement autocompletes, I can open the source code in Eclipse through "View Declaration," and I can see Google Maps in the Java build path.
import com.google.maps.GeoApiContext;
// Throws exception!
GeoApiContext.Builder builder = new GeoApiContext.Builder();
Why is this library visible at compile time but not runtime?
ETA: The application is packaged as an EAR file and deployed to a web server.
With the scarse details you provided, my guess is that you built a jar file and then ran java -jar myApplication.jar.
The default jar file only contains the classes of you own application, and not its dependencies. If you want to make an executable jar, you have several options. Some of the more popular are:
Use the shadow plugin, which will repackage the jar to make it "fat" with all its dependencies.
Use the standard distribution plugin that will create a zip (or tar) of your project and its dependencies. It will also a script for setting the classpath correctly.

To add External Spring jars for a spring project without Maven

I just downloaded the Spring IDE and Tool suite from Eclipse Market place.I am creating a sample Spring project using Spring core containers. I am following a tutorial video for that. I want to set build path by adding external downloaded Spring jars for that I have gone to build path of a specific project and I don't know where my downloaded external spring jars. I have searched in my local drive in java folder in programming files and also where my eclipse is saved. But I cannot find where my external spring jar files saved.
Kindly tell me the path where I can find external downloaded jars from eclipse Marketplace
Spring Suite tool is Rapid Application Development plugin, which helps to decrease spring configuration time and help you focus on core logic of your application.
Even though, As Martin Lippert Explain below, you might have to use Maven (Build Automation tool, And project dependency manager) for creating spring project.
people often use Maven for their dependency management (or Ivy or something else) and would like to use a specific version of the Spring framework (instead of the libs that are inside STS and used by STS itself). But you can define a user-defined Library that contains all the necessary Spring framework JARs and maybe others and just add that user-defined library to each project. Would make it a bit easier as adding several JARs all over again.
Maven uses a filesystem tree as a repository to store jar files with there metadata(dependency, version, etc.) which is located (by default) under your home or My Document Path within .m2 directory (folder).

How to add jar to Google App Engine with Eclipse?

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.

How to compile a web project with Weblogic dependencies

I'm trying to implement SSO for a web project that will be deployed on Weblogic. In the examples that I found the user identity is retrieved using weblogic's inner classes e.g. :
import weblogic.security.Security;
...
Subject subject = Security.getCurrentSubject();
I believe a jar containing this "weblogic.security.Security" is in weblogic's classpath, but how am I supposed to compile the project? This dependency is not available from maven so there's no way to mark it as "provided".
Is there a common solution to this problem or I should get this jar from the weblogic directory and add it to the project's classpath?
Thanks
You will have to get the jar, add it to your local maven repository (or your organisation one, generally nexus or artifactory) and set the scope as provided.
See http://docs.oracle.com/cd/E21764_01/web.1111/e13702/maven_deployer.htm for an example on how to deploy your jar in your local maven repo.

IntelliJ not using external Jar's if not added via Maven when running JSF application?

I have a very simple line of code that is called from an .xhtml file ( in a JSF 2.0 project ) which looks like:
try {
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception e) {
e.printStackTrace();
}
So in my C:\ folder I have the file: mysql-connector-java-5.1.24-bin. I have added this jar as an extenral jar in IntelliJ Idea 12.
When I start my application like this, I get the "Class not found exception." However, when I add this dependency to my pom.xml file:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
I get no exception and class is loaded fine.
Here is a screenshot:
The jar file I underlined with green is the Jar added externally from **File -> Project Structure -> Libraries **
The one I underlined with red is the jar file that is downloaded by maven.
My question is: Why do I get the Class not found exception, although I have added the Jar externally?
In case I require a Jar file that I will not be able to download via maven, what am I supposed to do?
One extra information: I created a sample Java project with one class only, and I added this external Jar file with the method I explained above. No maven, no tomcat, nothing. Just a simple main method. I ran this same code, and it worked fine. So it has to have something to do with IntelliJ and one or more of:
Maven
Tomcat ( Deploying a War file )
Maven and Tomcat?
Edit: When I add the external Jar only and try importing a class from com.mysql.jdbc... IntelliJ IDEA does not come up with any warnings / errors. It finds the class, however after the deployment, it seems like the Class can not be found anymore.
The webapp is deployed and run on tomcat. Tomcat doesn't care about the jars in the IntelliJ source project. What matters to tomcat is the set of jar files under the WEB-INF/lib directory of the deployed webapp. Only those jars are in the classpath of the webapp.
I don't know very well how IntelliJ builds and deploys webapps, especially when Maven is in the picture. But I'd guess that it only deploys the jars that are in the compile and runtime scopes of the Maven project. For an external jar, it's probably possible to configure the IntelliJ project in order to include it in the built and deployed archive.
But anyway, if you use Maven, you must be able to build your war without using IntelliJ. That's the whole point of a build tool: it's used to build a project without an IDE. So all the necessary jars must be known by Maven. So the jar, if not available in the Maven central, should be added to your own local Maven repo (at least), or to your company's private repo.

Categories