How to start a Java EE 6 project in Eclipse - java

I have recently begun learning Java EE 6 after doing Java SE for some years now (together with other languages). I have bought different books about Maven, EJB, JSF, Glassfish and so on (not read them all yet though), and I feel absolutely overwhelmed of the complexity of just making a project.
How would I start a project from "scratch" (Not copying a from an example in the book) in eclipse? I can choose from Dynamic Web project, Maven project, EJB project, EAR application etc. Which should I use if I want to make an application that has EJB/JPA and JSF inside (web application)?
I know about the Standard Maven Directory Layout and archetypes, however I do not know which to choose if this is the way to do it. Of course, I understand that there are many ways of doing it and it is dependent on what I will use it for, but I am learning Java EE 6 so I want to at least have some knowledge of how to set up a basic project as well.
I am also rather overwhelmed by the POM file. How do I know which dependencies I need just to get started with a web application? Or is this included in the correct archetype (if I find one?)
I am using Glassfish as application server, Eclipse, EJB, JPA and JSF. Currently learning from book examples only.

Well, a complete answer to your question would pretty much need an entire book on it's own, but here's some pointers.
The notion of Java EE applications represents any kind of Java application that uses one or more of the Java EE APIs implementation and needs a Java EE application server to run. Java APIs are those such: Servlet,JSP, EJB, JPA, etc. Implementations of these APIs are usually provided by the application Server (like Tomcat has servlet and JSP implementations, Glassfish has those as well plus EJB, etc). There are, let's say non-application server Java EE APIs implementations like Hibernate's implementation of JPA. So in short if you use some of these APIs implementations in your app and deploy it into some application server like Tomcat, Glassfish, JBoss AS, etc, you have a Java EE application.
I've told you all this because in reality, most of the project management tools (like Eclipse, Maven, etc) use these subdivisions like Web-Application, EJB Module, Dynamic Web Project etc, as a custom denomination that establishes the ball park of what set of Java EE APIs your application will use. I.E. a Web Application or Dynamic Web Project will more likely use Servlet and JSP APIs, an EJB Module will (duh) use the EJB APIs and so on. These are, as I said, ball park denominations. A Dynamic Web Application can use EJB or JPA as well, it's not like it's written in stone.
When you use either of these tools such as Eclipse or Maven, to start one of these above mentioned, denominated projects, such as a Dynamic Web Project in Elcipse or a Standard Web Application with Maven, that respective tool will add the common sense dependencies to your template project (like Servlet and JSP in this case), and then let you handle it from here.
The idea here is to understand each type of template of your chosen tool before you use it at the start of your project. Like if you decide to use Eclipse and start with a Dynamic Web Project, you should check out what exactly Eclipse asks of you and does when you do that (it will ask you for the location of a locally installed Java EE application server from where it will pull in such dependencies as a Servlet Implemnentation and a JSP implementation amongs others). Or if you chose Maven, and start a standard web project, you should check out what dependencies it adds to your project via the pom.xml file, what the basic functions of THAT pom.xml file are, etc. You should also check out how each of these tools deploys your application when you tell it to do that, does it use some hot deploy feature of your Java EE server, does it need credentials to access that server first etc.
My brief advice is to start with the very basic example: Install a Tomcat (version 7 is recommended), start Eclipse for Java EE Development, add that Tomcat installation to your Eclipse config, start a Dynamic Web project that uses that Tomcat as Application server, and just do some basic servlets and JSP pages, then use Eclipse to deploy that sucker to tomcat and run it. Then you can branch off to more advanced stuff, like doing the same thing with Maven, integrating a Maven project with Eclipse, adding some more Java EE Stuff (like a JPA Implenentation), etc etc.

http://blogs.oracle.com/arungupta/entry/web_app_development_using_java provides a complete explanation of how to create a Java EE 6 project using Eclipse and GlassFish.
Alternatively, you can also NetBeans to create a Java EE 6 project as explained at:
http://blogs.oracle.com/arungupta/entry/screencast_37_java_ee_6

I can choose from Dynamic Web project, Maven project, EJB project,
EAR application etc. Which should I use if I want to make an
application that has EJB/JPA and JSF inside (web application)?
Before EJB 3.1 spec it was not possible to deploy EJBs in war files, but now, you can. So for a quick prototype you can use a war file rather than an ear file if that's all you require and you are more familiar with it. Of course you can also deploy it in an .ear file.
I believe the Dynamic Web project in Eclipse generates a .war file that you can deploy in your AS.
If you are using Maven, you can also create your project with Maven beforehand, and then import using Maven (via M2Eclipse plugin). This works pretty well for me. Then, with the Glassfish Maven plugin, you can deploy your app to Glassfish via command line.
I am also rather overwhelmed by the POM file. How do I know which
dependencies I need just to get started with a web application? Or is
this included in the correct archetype (if I find one?)
You need the Java EE dependencies at least in a "provided" scope as the server will probably provide these for you. This way they will be available for you in compilation mode. For instance, the one for the JBoss Java EE 5 stack is as follows (not sure about the GlassFissh dependency, but the important stuff is not to include the library in your .ear / .war in order to avoid library conflicts):
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>javaee-api</artifactId>
<version>5.0-1</version>
<scope>provided</scope>
</dependency>
Added: As you said, this can be overwhelming at the start, I think this is a good resource on how to setup a Glassfish / Java EE project. Pick the bits you are interested on and good luck.

The first thing I'd ask you is why you're learning so many technologies. If your purpose is getting a job, in my experience, there's a trend in companies away from Java EE and towards simpler solutions. For example, application servers are overkill for most organizations and Tomcat (or another web server) with helper technologies like Spring and standalone JMS servers replacing full-featured and much more complex Java EE servers.
I think you'd be better served focusing on core concepts and simpler technologies as you get started and staying away from IDEs, complex builds, EARs, etc. Instead, start with one or two technologies (e.g. a hello Tomcat app with a simple Ant build) and gradually add each technology in as you find the need. Better yet if you have a real world problem to solve, even if it doesn't pay.
Most companies (at least those with a technical clue) would rather hire a programmer who understands what value a technology like JSF adds and at what cost of complexity than someone with a lot of acronyms on their resume.

How to start a new web app project in Eclipse with Maven?
Just type "archetype:generate" and select JSF + EJB/JPA if available.
This one seems fine: 360: remote ->
org.jboss.weld.archetypes:weld-jsf-jee (Weld archetype for creating a
Java EE 6 application using JSF 2.0, CDI 1.0, EJB 3.1 and JPA 2.0
(persistence unit included))
The Appfuse are quite more complete and may embed stuff you will never
use and that you will have to remove.
After that mvn eclipse:eclipse to be able to load the project in eclipse.

Related

Java - how are Akka, Play, Vertx etc. applications deployed?

I've been reading about some of the (relatively) new application frameworks for Java such as Akka, Play and Vertx. I can't find a clear answer however on whether or not applications created with these frameworks are deployed like traditional EE applications? That is, are they packaged as WAR/EAR files and deployed to an application server like WebSphere? I my mind, a lot of the WAR/EAR infrastructure was built with traditional EE apps in mind.
In there default they are not deployed like normal EE Applications. These Frameworks try to simplify things and make writing code faster and easier and so they most of the time have there own deployment mode and bring there own web server. Also they follow more the Docker approach of having fat jars and be able to be used as micro service.
So from my point of view it looks like this (could be wrong I did not use them):
Akka its possible to add to an WEB-INF/lib in an war file
Play native installer is recommended. They dropped the war possibility but there seems to be an github plugin
vert.x seems no support for ear or war files

How to add a plugin-architecture in my Java application? [duplicate]

How would you implement a Plugin-system for your Java application?
Is it possible to have an easy to use (for the developer) system which achieves the following:
Users put their plugins into a subdirectory of the app
The Plugin can provide a configuration screen
If you use a framework, is the license compatible with commercial developement?
First you need an interface that all plugins need to implement, e.g.
public interface Plugin {
public void load(PluginConfiguration pluginConfiguration);
public void run();
public void unload();
public JComponent getConfigurationPage();
}
Plugin authors should then bundle their plugins into JAR files. Your applications opens the JAR file and could then use an attribute from JAR manifest or the list of all files in the JAR file to find the class that implements your Plugin interface. Instantiate that class, the plugin is ready to go.
Of course you may also want to implement some kind of sandboxing so that the plugin is restricted in what it can and can not do. I have created a small test application (and blogged about it) that consists of two plugins, one of which is denied access to local resources.
Use OSGi.
It is the foundation of the Eclipse plug-in system. Equinox is Eclipse's implementation (licensed EPL) and Felix is the Apache Project's implementation (licensed Apache Public License).
Eclipse provides a concrete example that OSGi can cover the points you mentioned (or you could just build your application on top of Eclipse RCP if you want a full Eclipse/SWT/JFace stack).
Since 1.6, there's been java.util.ServiceLoader which can be used if you want to code your own simple system.
But if you want anything more than basic features, use one of the existing frameworks.
Use PF4J.
It has support for Web, Spring and Wicket.
Easy to use and build the applications
There is also JPF (Java Plugin Framework).
I worked on OSGi for a week--an intense, nothing but OSGi week. At the end it was like a bad dream but I learned a lot.
I was able to get OSGi working (not easy, all examples are out of date, everything on the net is at least three years old if not five), but I had serious trouble getting it integrated into an existing project because of issues with the jar manifests.
In short, there are only a few obscure tools used for building manifests and they are not well documented (BND Tools is hardly obscure, but it is designed for a certain process in Eclipse). Also, most of the OSGi information available is not targeted towards application developers who have an existing desktop application.
This makes a lot of the context for the information foggy or inappropriate. Neil Bartlett's blog posts were the biggest help, but even those failed to get a working system (I grabbed some code from the Felix tutorial and pieced it together to get the embedded framework rolling). I found his book draft that he posted for free years ago, which is excellent, but the examples in Eclipse do not work because of changes in Eclipse OSGi support.
I think that recommending OSGi for solving the above stated problem is extremely poor advice. OSGi is "the right choice" but for a scenario as the one above, I think either JPF or some homegrown minimalistic framework is sufficient.
Years ago I started a project like that and I hope soon will be ready.I got inspired by projects like NetBeans and Eclipse but meanwhile it changed to something a little bit different. OSGi looks like a good choice now, but I didn't had a chance to compare it with my project.It is similar with JPF mentioned above, but in the same time different in many ways.
The basic idea which motivated me is to be as easy as possible to build Java application, with no separation between web applications, desktop applications or applet/JWS applications(of course this doesn't cover the UI - yet) as a core functionality.
I built the project with a few goals in my mind :
it doesn't matter if you build a web application or a desktop application you should start the application in the same way, a plain main method, No fancy web.xml declaration(not that I'm against having a standard web descriptor, but it doesn't go well with a plug-in system, where you add "servlets" - I call them RequestHandler(s) - dynamic at your will).
easy to plug in "extensions" around an "extension point" - something from Eclipse but a different approach.
self-deployable, since all the plugins are registered(XML files) the application must be self-deployable independent of the build system - of course there is an Ant task and a Maven MOJO which are the links with the ourside world, but in the end it calls the application and instruct it to self-deploy itself at a specific location.
borrowed from Maven, it can download code from repositories(including Maven 1 & 2 repositories) so your application can be deployed as a single small jar as long as you have access to the repositories(useful sometime, and basically this provides support for auto-updates - don't you love the idea to be notified by your web application that there is a newer version, it was downloaded and it just needs your permission to install it? I know I love that).
basic application monitoring about system health, email notifications in case of failures

Implications of building a java program against the jars of one web container and deploying it in another

What are the implications of building a java program against the jars of one web container (say Jetty) and running it in another (say Tomcat)?
I have an application which I run in Jetty durring development but which is deployed into a tomcat server for production (Why? because it seems easier to develop without having to run a whole tomcat server.)
You should compile against only the official Java EE API's for the level you target, for any non-developer builds. Preferably by a build engine. Preferably on a different operating system than you develop on.
For a web application this mean the appropriate servlet API as downloaded from Oracle. Similar for an enterprise application.
In my experience this is the best way to keep it straight.
Edit: Java EE SDK is available from http://www.oracle.com/technetwork/java/javaee/downloads/index.html. If you need an older version than Java EE 6, then follow the "Previous Releases" link.
You can get issues such as MethodNotFoundError. You can usually resolve these by making sure versions of jars installed on the servers match.
you typically want to develop where you deploy. It might be slightly harder to develop with tomcat vs jetty, but you have identified a potential mess of a problem with jar conflicts, so doesn't it seem worth it to develop with tomcat, since you deploy to tomcat?
Also, typically the pain of developing against tomcat/your container of choice is mitigated by putting in the time to write a ant (or other) task that will deploy your code to your development container. The work cycle bemoes
1) Write new code
2) make sure tests pass
3) run your 'redeploy' script
4) poke around in the running instance
You probably want to do that.
Finally, in the spirit of loose coupling, you probably do not want to depend on a container-specific libraries if you can avoid it; only do that as an absolute last resort.

How to develop a modular enterprise application using just GWT

Hi
I want to design and develop a big enterprise application using just
GWT in client side.
I want to break this enterprise application into parts and I call each
of them a module (or bundle or portlet or whatever!).
These modules might have relation with each other and might call some services that
exists in other modules (in both client and server side).
The problem is, These modules must be Designed , Developed, Compiled
and Deployed Independently and Dynamically and they will be placed and
shown together in one context on the client and the dependencies
between modules should be manageable (in both client and server side).
What can I do? What kind of technologies I can use to build an enterprise application like this?
When you develop an application that is not divided into parts (In the way that i mentioned) you can easily deploy your application after building your project, but when you change just one form in your application you have to build the entire application again, and deploy the entire application.
In this application I cannot stop the server to deploy the application again, I want to change and deploy that part of application that is needed to be changed not the entire application!!!
Of course I have searched about the way that I can solve my problem!!!
I have found that I can use OSGI on server side because it provides modularity at software construction level and helps me to manage life cycle of modules and many other benefits that you know!
And I have found that I can use Gadgets on client side.
What do you think? Are they good choices?
If they are good choices, how can I start? I know that we have different kinds of implementations of OSGi, like Apache Felix, Eclipse Equinox and Knopflerfish. Which one is good for this choice?
How GWT and OSGi can be integrated? How can they interact with each other?
Unfortunately what you want to do is not fully possible with GWT.
OSGi is a modularity solution for Java, or more accurately the JVM. A GWT client application does not run on the JVM, it runs on the browser in a JavaScript environment. Therefore OSGi cannot be used to create runtime-assembled modular GWT applications.
A GWT application can be modular at the source level, but the modules must be assembled into an application at build time. The resulting runtime is monolithic.
However, it's perfectly possible to use OSGi to host the GWT servlets, and you can use the full power of OSGi runtime modularity on the server side.
As an alternative you may want to look at Vaadin. This is a web framework that uses GWT to provide widgets, but the logic of the application runs on the server. As a result, it does support full runtime modularity through OSGi bundles. There is a cost with this approach though: your web application is quite chatty, with lots more communication going between the browser and the server than in GWT or in a traditional web application. It's possible that this approach will not scale to very large numbers of users.
As for whether to use Equinox, Felix or Knopflerfish... it really doesn't matter. Stick to the specification, and you can easily switch between implementations.
I did just this two years ago: OSGi and GWT for no downtime deployments of project modules.
Verdict: Don't do it unless you really must.
In short, OSGi is a beast and retrofitting an existing application for it is far from trivial. You're no longer making .war files (.ear now) and can't use the standard jars and Maven repositories you used before. Now everything needs to be a bundle. Trouble is, a lot of stuff (GWT, Spring, tons of libs) are not bundles! And you'll need to find them in an enterprise bundle repository or, even more fun, start rebundling 3rd party sources them yourself. Better yet, telling the other devs to rewrite everything that uses their favorite lib because bundling it would be too complex.
The GWT part didn't take that much work. The way contexts for modules were handled in gwt-servlet had to be modified so each module could find it's context on the server. We also had to make a way for most of the GWT services to register/unregister on load and a discovery service so they could know who else was out there.
Now the other pain: project explosion.
Let's say you had 20 modules you wanted to deploy independently. Well, to start with they're probably more coupled than you'd like, so better spent a few weeks breaking them into independent Maven projects and pushing common parts to a lib project. But now, you've got tons of dependencies to keep track of. When someone tweaks your lib project, do you need up upgrade every project or just 7 of them? In the classic stop the world deployment, you only had one version of all your code. Now, you need to decide if that forgot password form being upgrades will require you to also upgrade your index page module. You'll have a ton of version numbers to make up and keep track of. In our case, we quickly had 55 Maven projects building all the time in our CI server. This meant some checkins could trigger 55 builds. Eek.
Finally, JSON interfaces.
We used GWT RPC. It's magical. Write an interface and everything just works. It's also serialized and gzipped over the wire too. Awesome. But, the serialization policies depend on object and string lookup tables that are built at compile time per module. So, project A cannot RPC to project B. Boo. We chose to use JSON due to the graceful degradation, that is not failing when new unrecognized properties were present on objects. This means you'll again need a way to keep all the backend service calls coherent in the versions of the JSON they are expecting and can handle. Better simulate that live upgrade beforehand too.
So, final word: possible, but why? Do you really need OSGi to hot deploy modules because you're running a 1000% uptime business critical application? Or does your boss/architect just refuse to accept that 99.999% is good enough? You probably don't need that uptime and can achieve nearly 100% uptime with a good proxy to let you take instances in/out of the balancer pool. Also, don't forget that even if you can upgrade your projects live on the fly, I hope you've got a way to upgrade your database on the fly without dropping a single transaction.
I think you are setting yourself for more headaches than it is worth.
I would go with deploying the whole thing at a pop. If not you will end up with mismatched pieces of the application that are out of sync with each other. GWT has both Client and Server components and they need to be deployed together. If you have a zero downtime policy then you probably have load balancing in place.
I would use the load balancing software to deploy the new version of the app. Turn off one side (by diverting all traffic to the other side) deploy to it, do a quick smoke test, switch all traffic to the new side and repeat with the old side.

What do I need to write Java-based web applications and test them on my personal computer?

I currently have Apache HTTP Server, but I'm guessing I'll need Tomcat (and then have to configure it in a way that makes it not open to the public), a Java JDK (which I already have, but should probably update), and an IDE (I have Eclipse). But what else should I have or know before starting?
Lets see... you'll need:
A JDK. Preferably the Standard Edition, unless you plan on using GlassFish as a J2EE server. If you do use the standard edition, you need to include the jars from your servlet container/J2EE stack to access J2EE libraries.
A Servlet container. Apache Tomcat is a popular one, and Eclipse already has integration support for it. Keep in mind that Tomcat is not a complete J2EE stack... you need something like JBoss Application Server or Apache Geronimo for that.
(Semi-Optional) A web server. Apache Web Server surprisingly works well with Apache Tomcat or Apache Geronimo... it's almost like they were made by the same people! Eclipse has built-in support for Tomcat and doesn't appear to require a web server because of it. I could be wrong, though.
(Optional) An IDE. If you use Eclipse, get the version for Java EE Developers.
(Optional) A Database. MySQL and HSQLDB are popular ones for Java, at least for small to medium applications. Keep in mind that you also need to download the JDBC drivers for your database.
(Optional) A revision control system. Even on a single-developer project, a revision control system can save your hide if you accidentally remove code or a file that you shouldn't. There are several choices here; Subversion is my personal choice, along with an Eclipse plugin for it, such as Subclipse.
I'd recommend having a database server installed as well- you can go pretty far with the various pure Java implementations, but anything significant will probably benefit from having a copy of MySQL around.
I'd also get some sort of version control going- tortoiseSVN works really well for windows. Even if you're just goofing around for yourself, getting in the habit of using it will save time and heartache down the road.
If you're trying to stay pretty simple you have everything you need; a servlet container.
The IDE of course helps.
I would recommend Eclipse's Web tools project as it will allow you to code, and then hit a button that will put that code on tomcat all from the IDE.
http://www.eclipse.org/webtools/
As Bogdan mentioned maven is a dependency management tool but depending on your level of comfort I'm not sure if you want to muddy the water with that yet.
Another project that uses Maven is appfuse (http://appfuse.org/display/APF/Home) and will help you get off the ground quickly for generating a webapp. It will give you a webapp, a database, unit tests, dependency management, and a good skeleton to structure your project off of.
So, if you're just wanting to play around with some .jsps and a servlet or two I would stick with the IDE, but if you're comfortable with web applications, MVC, and build tools I'd recommend appfuse.
That's all you need from the tools.
Then, you need to google some tutorial on using servlets and jsp pages. Even documentation bundled with tomcat is good enough.
You'll probably also want a database like MySQL or HSQLDB. You could replace Tomcat with Jetty which is often easier to get class changes to reload without restarting the server.
You can do everything from within NetBeans if you're willing to switch IDEs (it's a relatively painless switch, but you might be able to create a similar setup in Eclipse). There's a Tomcat plugin for NetBeans that allows you to create, edit, run, and debug servlet and JSP web apps all from within NetBeans. There's a starter tutorial here. When you install the Tomcat plugin, you get lots of good example servlets and JSPs as a bonus.
I would go for maven.
It will give you a quick start in configuring your project (by using archetypes) and it will manage your dependencies.
Install it and run the archetype command to create your project.
mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
After that just use the maven eclipse plugin to configure your eclipse environment for that project (http://maven.apache.org/plugins/maven-eclipse-plugin/).
I currently have Apache HTTP Server, but I'm guessing I'll need Tomcat (and then have to configure it in a way that makes it not open to the public)
For simple Java based web applications, you need not have Apache installed unless you are too concerned about performance and want to gain by letting Apache serve static resources.
But what else should I have or know before starting?
A good IDE is most recommended, otherwise the development will become painful. Use a simple database as people have already suggested. I would go with MySQL because its neither too complex for a webapp nor too trivial.
Instead of Tomcat I recommend Jetty, that is also a Servlet-Container. In my experience it's easier to setup and maintain. Good enough to test your application.
Other than that you need a JDK (naturally). A database is optional, but if your web-app wants to save data it's the best option.
Step 1:you need to have J2EE installed in your system if not download it from http://www.oracle.com/technetwork/java/javaee/downloads/index.html here.
Step 2: Should have Apache TomCat server to run your web project on your system locally(local Server).
installation steps https://www.ntu.edu.sg/home/ehchua/programming/howto/Tomcat_HowTo.html go to this website.
Step 3:set Class path for java JRE.

Categories