I have web application which has a servlet and an ejb. The ejb is annotated #Local. In my servlet I used #Inject to inject the ejb. It woekd fine.
Then I created an ear with an ejb module and a web module. I deployed the ear and I got exceptions. Then I learned that I cannot use #Inject. So I used #Resource(lookup = "jndiname") and then it worked.
Then I created an independent web application and used the #Resource annotation to inject the ejb in the ear into a servlet. The war failed to load. So I thought to place the ejb jar inside the lib directory of the app server so that all applications in the app server can use them. I am using jboss eap 6.0. I placed the jar under jbosshome/modules/com/ejb/beginner/main/. It did not work. I got exceptions. Then I placed the ejb jar in jbosshome/modules/com/ejb/beginner/api/main/. Still no luck.
I want to learn how to create #Local beans so that applications in the app server can use. Is it possible?
I figured it out. If I want to use modules from another ear in other deployments I have to declare the dependencies in the MANIFEST.MF file like this
Dependencies: deployments.appname.ear.appname.jar
The following links help me figure it out
https://community.jboss.org/message/604576
https://docs.jboss.org/author/display/AS71/Class+Loading+in+AS7
i'm setting up a new multi module project structure. for now there is an ejb and an ear module and i want to add a web module for a RESTful web service. what is the best approach?
the application looks like this (modules and their content):
app (pom): the parent pom
core (ejb): ejb, hibernate, DAOs, entities, business logic
ear (ear): the module that creates a deployable container
now i would like to add a restful webservice module.
looking at the jboss-as-7 quickstart applications from http://www.jboss.org/jbossas/downloads there is always a war module which just bootstraps the rest service while the beans with jax-rs annotations are part of the ejb project (in my case core:
app
ejb (containing services + beans with jax-rs annotations)
ear
war (just containing a web.xml)
my issue with this approach is that the ejb module contains the services and the rest resources. wouldn't it be better to keep them separate? or is this the way to go?
otherwise: which approach would be better?
app
core
ear
rest (war which contains the rest resource beans and the web.xml)
or
app
core
ear
rest
rest-resources (separate container for the rest resource beans)
rest-war (war which only contains the web.xml)
From a JEE6/JAX-RS point of view the only thing you need a WAR file for is to actually have something to hand off to the application server to deploy. It can literally contain no resources (except for the ones required to make it a valid archive) and your application will still work as long as your classes are made available somewhere on the classpath. So, its really up to you to bundle up these classes as you see fit.
The one thing you will need is a class that extends javax.ws.rs.core.Application. This class is used as a marker/bootstrap to indicate to the application server that there are REST resources available in the web archive that need to be loaded. In most shops I've seen this class placed in the WAR project itself.
If you are really looking for organizational tips, I would recommend you start off by grouping all the REST resource classes together in their own project. Add as dependencies, any projects that are required for the resource classes to work (for example your core and ear projects). Add all three (rest, ear, core) as dependencies for your war project.
Then refine the model as needed, as you work your way through implementation.
There is a really awesome JBoss Maven Archetype for this sort of project.
1) Go to eclipse with maven integration installed
2) New -> Project -> Maven Project
3) Click Next
4) In Filter type in ee6
5) Out of the options that appear, use Group ID: "org.jboss.spec.archetypes", and Artifact ID: "jboss-javaee6-webapp-archetype"
6) Out will come a beautifully structured web application project with EAR connecting to a EJB project and a WAR project.
I have a Java EE web application that does not make use of EJBs. I am targeting Jetty/Tomcat for deployment some of the time and thus need a WAR packaging. However, I am also target JBoss and Websphere some of the time.
My understanding is that full-blown Java EE application servers can take either EAR or WAR formats. When would I use one over the other and why? I understand they are both standard compressed file formats and I have read 10 different snippets that try to explain them (including this one), but am no closer to understanding the pros and cons of each.
If you have only web modules - use WAR file.
If you have different Java EE modules - use EAR. Though you can deliver only web modules in EAR - there is no point in doing this (and you will need to do more complex deployment configuration)
The crucial point is if you need anything provided in an EAR file (which may contain said WAR file). If so, then it makes sense to deploy as an EAR.
It can also wire up some of the configuration you need to do manually in Tomcat etc. A typical example is the URL bound to the web application, where you need to override the default heuristic with a container specific configuration file for a WAR, but you can put it directly in the EAR configuration file.
Also, during development WARs can frequently be hotdeployed directly in exploded form, where EARs must be unpacked and deployed. For Glassfish from Eclipse the difference is quite noticeable.
In J2EE application, modules are packaged as EAR, JAR and WAR based on their functionality
If You are using only Servlet, JSP, GIF and HTML files. Then use .WAR
WAR:(Web ARchive) Web modules which contain Servlet class files, JSP
Files, supporting files, GIF and HTML files are packaged as JAR file
with .war (web archive) extension.
If you are using different different JAVA EE modules like (EJB + Servlet, JSP, GIF and HTML files + Other technology). Then use .EAR
EAR: (Enterprise ARchive) All above files (.jar and .war) are packaged
as JAR file with .ear (enterprise archive) extension and deployed into
Application Server.
I'm developing a Jboss web service that requires to access files which are in a folder of the project. When I deploy the web service, it creates a .jar, but the folder doesn't go inside of it, even if I added it to build path.
How do i tell jboss to place the folder inside of the .jar?
Thanks in advance.
I assume, because you explicitely say "jar" that your webservices are ejb endpoints, and not JAX-RPC servlet based webservices (because those would be packaged in a .war).
Unlike servlets, who are somewhat capable of finding files inside the project (as long as you can express their location as an offset to the web folder you can determine theire real location with ServletContext's getRealPath() EJB's don't have any "automatic" notion of directories.
So even if you could convince Eclipse to deploy files for you, I doubt it would help you much.
EDIT:
in a web-archive (.war) everything inside the web folder will be accessible by the servlets at runtime via the mechanism explained before. Do note however that files inside web are accessible via the web interface, except those inside the WEB-INF folder, so I'd suggest to at least store your files somewhere in a subdirectory of WEB-INF.
On a more global scale you should also ask yourself the question if you really must deploy these files with the application - and redeploy them with each redeployment. One solution in that case is to create some directory on the server (say c:/applicationfiles), create a JVM option e.g. -Dserverfilestore=c:/applicationfiles and have your application determine that directory with
String storebase=System.getProperty("serverfilestore");
The rest should be quite obvious. This solution will also work with you EJB services btw.
What is the difference between a .war and .ear file?
A WAR (Web Archive) is a module that gets loaded into a Web container of a Java Application Server. A Java Application Server has two containers (runtime environments) - one is a Web container and the other is a EJB container.
The Web container hosts Web applications based on JSP or the Servlets API - designed specifically for web request handling - so more of a request/response style of distributed computing. A Web container requires the Web module to be packaged as a WAR file - that is a special JAR file with a web.xml file in the WEB-INF folder.
An EJB container hosts Enterprise java beans based on the EJB API designed to provide extended business functionality such as declarative transactions, declarative method level security and multiprotocol support - so more of an RPC style of distributed computing. EJB containers require EJB modules to be packaged as JAR files - these have an ejb-jar.xml file in the META-INF folder.
Enterprise applications may consist of one or more modules that can either be Web modules (packaged as a WAR file), EJB modules (packaged as a JAR file), or both of them. Enterprise applications are packaged as EAR files ― these are special JAR files containing an application.xml file in the META-INF folder.
Basically, EAR files are a superset containing WAR files and JAR files. Java Application Servers allow deployment of standalone web modules in a WAR file, though internally, they create EAR files as a wrapper around WAR files. Standalone web containers such as Tomcat and Jetty do not support EAR files ― these are not full-fledged Application servers. Web applications in these containers are to be deployed as WAR files only.
In application servers, EAR files contain configurations such as application security role mapping, EJB reference mapping and context root URL mapping of web modules.
Apart from Web modules and EJB modules, EAR files can also contain connector modules packaged as RAR files and Client modules packaged as JAR files.
From GeekInterview:
In J2EE application, modules are packaged as EAR, JAR, and WAR based on their functionality
JAR:
EJB modules which contain enterprise java beans (class files) and EJB deployment descriptor are packed as JAR files with .jar extension
WAR:
Web modules which contain Servlet class files, JSP Files, supporting files, GIF and HTML files are packaged as a JAR file with .war (web archive) extension
EAR:
All the above files (.jar and .war) are packaged as a JAR file with .ear (enterprise archive) extension and deployed into Application Server.
war - web archive. It is used to deploy web applications according to the servlet standard. It is a jar file containing a special directory called WEB-INF and several files and directories inside it (web.xml, lib, classes) as well as all the HTML, JSP, images, CSS, JavaScript and other resources of the web application
ear - enterprise archive. It is used to deploy enterprise application containing EJBs, web applications, and 3rd party libraries. It is also a jar file, it has a special directory called APP-INF that contains the application.xml file, and it contains jar and war files.
WAR (web archive) files contain servlet class files, JSPs (Java servlet pages), HTML and graphical files, and other supporting files.
EAR (enterprise archive) files contain the WAR files along with the JAR files containing code.
There may be other things in those files but their basically meant for what they sound like they mean: WAR for web-type stuff, EAR for enterprise-type stuff (WARs, code, connectors et al).
JAR Files
A JAR (short for Java Archive) file permits the combination of several
files into a single one. Files with the '.jar'; extension are utilized by
software developers to distribute Java classes and various metadata.
These also hold libraries and resource files, as well as accessory files
(such as property files).
Users can extract and create JAR files with Java Development Kit's
(JDK) '.jar' command. ZIP tools may also be used.
JAR files have optional manifest files. Entries within the manifest file
prescribe the JAR file's use. A 'main' class specification for a file class
denotes the file as a detached or ‘stand-alone' program.
WAR Files
A WAR (or Web Application archive) files can comprise XML
(extensible Markup Language) files, Java classes, as well as Java Server
pages for purposes of Internet application. It is also employed to mark
libraries and Web pages which make up a Web application. Files with
the ‘.war' extension contain the Web app for use with server or JSP (Java
Server Page) containers. It has JSP, HTML (Hypertext Markup
Language), JavaScript, and various files for creating the aforementioned
Web apps.
A WAR file is structured as such to allow for special directories and
files. It may also have a digital signature (much like that of a JAR file)
to show the veracity of the code.
EAR Files
An EAR (Enterprise Archive) file merges JAR and WAR files into a
single archive. These files with the ‘.ear' extension have a directory for
metadata. The modules are packaged into on archive for smooth and
simultaneous operation of the different modules within an app server.
The EAR file also has deployment descriptors (which are XML files)
which effectively dictate the deployment of the different modules.
Refer: http://www.wellho.net/mouth/754_tar-jar-war-ear-sar-files.html
tar (tape archives)
- Format used is file written in serial units of fileName, fileSize, fileData
- no compression. can be huge
Jar (java archive)
- compression techniques used
- generally contains java information like class/java files. But can contain any files and directory structure
war (web application archives)
- similar like jar files only have specific directory structure as per JSP/Servlet spec for deployment purposes
ear (enterprise archives)
- similar like jar files. have directory structure following J2EE requirements so that it can be deployed on J2EE application servers.
- can contain multiple JAR and WAR files
Ear files provide more options to configure the interaction with the application server.
For example: if the hibernate version of the application server is older than the one provided by your dependencies, you can add the following to ear-deployer-jboss-beans.xml for JBOSS to isolate classloaders and avoid conflicts:
<bean name="EARClassLoaderDeployer" class="org.jboss.deployment.EarClassLoaderDeployer">
<property name="isolated">true</property>
</bean>
or to src/main/application/META-INF/jboss-app.xml :
<?xml version="1.0"?>
<jboss-app>
<loader-repository>
loader=nameofyourear.ear
<loader-repository-config>java2ParentDelegation=false</loader-repository-config>
</loader-repository>
</jboss-app>
This will make sure that there is no classloader conflict between your application and the application server.
Normally the classloader mechanism works like this:
When a class loading request is presented to a class loader, it first
asks its parent class loader to fulfill the request. The parent, in
turn, asks its parent for the class until the request reaches the top
of the hierarchy. If the class loader at the top of the hierarchy
cannot fulfill the request, then the child class loader that called it
is responsible for loading the class.
By isolating the classloaders, your ear classloader will not look in the parent (=JBoss / other AS classloader).
As far is I know, this is not possible with war files.
J2EE defines three types of archives:
Java Archives (JAR) A JAR file encapsulates one or more Java classes, a manifest, and a descriptor. JAR files are the lowest level of archive. JAR files are used in J2EE for packaging EJBs and client-side Java Applications.
Web Archives (WAR) WAR files are similar to JAR files, except that they are specifically for web applications made from Servlets, JSPs, and supporting classes.
Enterprise Archives (EAR) ”An EAR file contains all of the components that make up a particular J2EE application.
To make the project transport, deployment made easy.
need to compressed into one file.
JAR (java archive) group of .class files
WAR (web archive) - each war represents one web application
- use only web related technologies like servlet, jsps can be used.
- can run on Tomcat server
- web app developed by web related technologies only jsp servlet html js
- info representation only no transactions.
EAR (enterprise archive) - each ear represents one enterprise application
- we can use anything from j2ee like ejb, jms can happily be used.
- can run on Glassfish like server not on Tomcat server.
- enterprise app devloped by any technology anything from j2ee like all web
app plus ejbs jms etc.
- does transactions with info representation. eg. Bank app, Telecom app