EJB Hello World project with Jboss server

Hi everyone, today i have experienced it can be rather challenging to setup a EJB (Enterprise Java Beans) project from scratch so i decided to make a little tutorial. To do this i will be using the Intellij IDE and Jboss as my server.IMPORTANT: DO NOT USE ANY SPACES IN YOUR PATHS! THE SERVER WILL HATE YOU FOR IT! :)


Prerequisites:
Create a new project from scratch:

  • In your Intellij IDE go to File -> new project
  • Create project from scratch -> next
  • name your project "ejb" and put it on c:\\ for example, as long as there are no spaces. Leave all other settings as they are (create java module checked) -> next
  • Create source directory src -> next
  • In the the current window you are able to select the facets you'd like to add to your module, select application server, enterprise javabeans (EJB) and javaEE application. make sure the EJB version is set to 3.1 and that you select your jboss 6.1.0 server for application server. note: uncheck Web application! as we only want to create the ejb side (backend in this tutorial)
  • click finish and intellij will download all needed jars for you! :) 
In a perfect world this would be all there is to do, unfortunately when we try to run our application we will get the following error: Cannot build 'ejb.ear exploded' artifact: it includes itself in the output layout.

Allright, no stress we will fix this little inconvenience in no time, I promise. Go to file -> project structure (ctrl+alt+shift+s keep this in mind you will probably need it tons of times in the future). In the project dialog go to Artifacts.

Notice that the ejb:ear exploded artifact contains ejb.jar and that it includes ejb:ear (so it includes itself..). Remove the ejb.jar folder by selecting it and clicking the minus icon. Next expand the Artifacts element under Avaible elements and double click on ejb:ejb exploded. Problem solved! ejb.jar file now contains the correct artifact.

Intellij now provides a quick fix to register the ejb facet in your application.xml, just click fix .. oh do I love intellij! :). Click apply and ok. Notice that your application.xml now looks like this:



    
        ejb.jar
    


Intellij automagic!
Now go to your run configurations it should look like this
click ok and run your application
...
Artifact ejb:ear exploded: Artifact is being deployed, please wait...
Artifact ejb:ear exploded: Artifact is deployed successfully
...

browse to localhost:8080 and you should see the following page:

Now let's add some EJB stuff. Add the following interface and class to the src map:
This is our stateless helloworld EJB bean.
public interface HelloUser {
    public String sayHello(String name);
}
import javax.ejb.Stateless;

@Stateless
public class HelloUserBean implements HelloUser { 
    public String sayHello(String name) {
        return name;
    }
}


That's it! we now have a running Java EE container with EJB support :)
Next up we'll be adding a webclient to this setup to test our EJB bean.. stay tuned and happy learning! The next part has arrived!