Generic War file just like API concept - java

AS you may know that when an API is prepared, it can be used in any project just by adding it into classpath in J2SE. I wonder if it is possible to create a web page and compiled it into a war file just like an API definition so that It can directly be used within an ear.

No, you can't do that using a war file, because a war file would define a new web application separated from the other ones.
But since the servlet 3.0 spec, you can do that with a jar file placed in the WEB-INF/lib directory of your war file, using the META-INF/resources directory of the jar. See this question for details.

Related

usage and significance of .Classpath in Application EAR

I am currently working on an EAR application which has a WEB project and EJB project in it.
To work on eclipse with local set up i do so many build path setups and all.
All these buildpaths show up in .classpath of the project.
Now when i export the EAR and deploy on Server everything works on server.
My doubt is how does server know about my local system paths which are present in .classpath of my project.
Does it mean .classpath has no significance at runtime?
Please explain.
.classpath file is eclipse specific, it will not be bundled with the EAR. Eclipse uses it to set the classpath for the project.
An EAR has a well defined structure and Servlet container understands that structure, using it's knowledge it prods around in the EAR file and extracts classes to load in the runtime.
Obviously server doesn't have any knowledge of your local environment. An EAR (or WAR or JAR) file is a simple archive file (with some specification or the file order and all). You can unzip it using any utility (something like 7zip). Check the structure of the packed EAR, that will give you a better understanding of what goes to your server.

Referring shared Java EE library for EAR application in weblogic

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. :)

How to Deploy Java EE Project which reference external JAR files in weblogic

I have a web application which consist of JSP pages, Servlet and Consumes Web Services.
It also references apache axis and excel libraries.
Now I want to deploy my application directly in Weblogic server
How do i do that.Whcih archive shud i make WAR or JAR??
ALso how to ensures that it covers all the referenced libraries.
I have made my application in Jdeveloper, but I dont want to deploy it using Jdevelper..
I would package my solution as a .war file, containing all dependent .jar files.
That way your solution is self-contained. You can deploy to an app server containing other apps with their own versions of your libraries (dependent or developed). If you put the dependent jars directly into the app server (as you can do), then you're forcing those versions on all applications deployed, and that could well cause you grief.
The downside is that your developed .war file can become sizable. It's not normally a major problem, and I wouldn't worry about it until it's identified as an issue.
A JAR-file cannot contain a JAR-file, so that option is out. Since you mention JSPs and servlets a WAR would seem the appropriate option, although an EAR with a WAR and several JARs could also be a way forward...
Cheers,
Consider a WAR with your JAR files in WEB-INF/lib. Or, create an EAR with APP-INF/lib folder.

Difference between jar and war in Java

What is the difference between a .jar and a .war file?
Is it only the file extension or is there something more?
From Java Tips: Difference between ear jar and war files:
These files are simply zipped files
using the java jar tool. These files are
created for different purposes. Here
is the description of these files:
.jar files: The .jar files contain
libraries, resources and
accessories files like property files.
.war files: The war file contains
the web application that can be
deployed on any servlet/jsp
container. The .war file contains jsp,
html, javascript and other files
necessary for the development of web
applications.
Official Sun/Oracle descriptions:
The J2EETM Tutorial: Web Application Archives
The Java Archive (JAR) File Format: The Basics
Wikipedia articles:
WAR file format (Sun)
JAR file
WAR stands for Web application ARchive
JAR stands for Java ARchive
A .war file has a specific structure in terms of where certain files will be. Other than that, yes, it's just a .jar.
You add web components to a J2EE application in a package called a web application archive (WAR), which is a JAR similar to the package used for Java class libraries. A WAR usually contains other resources besides web components, including:
Server-side utility classes (database beans, shopping carts, and so on).
Static web resources (HTML, image, and sound files, and so on)
Client-side classes (applets and utility classes)
A WAR has a specific hierarchical directory structure. The top-level directory of a WAR is the document root of the application. The document root is where JSP pages, client-side classes and archives, and static web resources are stored.
(source)
So a .war is a .jar, but it contains web application components and is laid out according to a specific structure. A .war is designed to be deployed to a web application server such as Tomcat or Jetty or a Java EE server such as JBoss or Glassfish.
A .war file is a Web Application Archive which runs inside an application server while a .jar is Java Application Archive that runs a desktop application on a user's machine.
A war file is a special jar file that is used to package a web application to make it easy to deploy it on an application server. The content of the war file must follow a defined structure.
.jar and .war are both zipped archived files.
Both can have the optional META-INF/MANIFEST.MF manifest file which hold informative information like versioning, and instructional attributes like classpath and main-class for the JVM that will execute it.
.war file - Web Application Archive intended to be execute inside a 'Servlet Container' and may include other jar files (at WEB-INF/lib directory) compiled classes (at WEB-INF/classes (servlet goes there too)) .jsp files images, files etc.
All WAR content that is there in order to create a self-contained module.
war and jar are archives for java files. war is web archive and they are running on web server. jar is java archive.
Basicly both compressed archives. war is used for web application with a specific directory structure.
A JAR file extension is .jar and is created with jar command from command prompt (like javac command is executed). Generally, a JAR file contains Java related resources like libraries, classes etc.JAR file is like winzip file except that Jar files are platform independent.
A WAR file is simply a JAR file but contains only Web related Java files like Servlets, JSP, HTML.
To execute a WAR file, a Web server or Web container is required, for example, Tomcat or Weblogic or Websphere. To execute a JAR file, simple JDK is enough.
War : For web-applications
Jar : For desktop applications
OR
War : Working on browser
Jar : Working on machine
War -
distribute Java-based web applications. A WAR has the same file
structure as a JAR file, which is a single compressed file that
contains multiple files bundled inside it.
Jar -
The .jar files contain libraries, resources and accessories files
like property files.
WAR files are used to combine JSPs, servlets, Java class files, XML
files, javascript libraries, JAR libraries, static web pages, and any
other resources needed to run the application.
Jar:- jar contain only .class
war:- war contain html, js, css and .class also jsp and servlets pages
JAR files allow to package multiple files in order to use it as a library, plugin, or any kind of application. On the other hand, WAR files are used only for web applications.
JAR can be created with any desired structure. In contrast, WAR has a predefined structure with WEB-INF and META-INF directories.
A JAR file allows Java Runtime Environment (JRE) to deploy an entire application including the classes and the associated resources in a single request. On the other hand, a WAR file allows testing and deploying a web application easily.
Also, using an embedded container, you can run a JAR file directly whitouh setting up a web server like when running your java app with spring boot. However, for a WAR file, you need to set up first a web server like Tomcat for example.

When releasing a java spring web application, what are the steps to deploy/release?

If I make a spring mvc application, what are the things I have to do to deploy the application? (say its a commercial application)
What options do I have? Do all applications compress the classes into a .war file?
/WEB-INF/web.xml, appname-servlet.xml, etc.
/WEB-INF/jsp/*.*
/WEB-INF/appname.war ???
Done correctly, the build process will create a war which can be dropped into any servlet container (Tomcat, Jetty, Glassfish).
If you use external source files, those would have to be configured.
If you use advanced features provided by the servlet container, the server would have to configured as well.
Spring application is no different from any java web application when deploying. but generally the only thing I have to do is flip the order of test spring config with the actual one.
Any IDE could create the WAR file for you. As you've said the configuration xml files go to /WEB-INF folder and jsp files (by default) to /WEB-INF/jsp/. You also need to put all required jar files in /WEB-INF/lib folder. Compiled classes will go to /WEB-INF/classes, but let the IDE do that for you.
The war file shouldn't be in the /WEB-INF folder. In Tomcat for example you need to copy it to the webapps folder.

Categories