How to create a EJB 3.x project using Maven in Eclipse – Part 2

25 January 2013 By Nithya Vasudevan 25,108 views 9 Comments
9 Flares Twitter 0 Facebook 3 Google+ 6 9 Flares ×

In Part 1 of this tutorial, we saw how to develop and deploy EJB in JBoss AS using Maven in Eclipse. In this EJB Maven tutorial we will see how to develop and run the EJB client (using Maven) to access the ejb.

Create New Maven Project in Eclipse

  • Open Eclipse IDE and create a new Maven project which can be done in three ways,

    • Right click on Project Explorer -> New -> Other… -> Maven -> Maven Project
    • File menu -> New -> Other… -> Maven -> Maven Project
    • Click on the down arrow on New icon on toolbar -> Other… -> Maven -> Maven Project
  • Select Maven Project and click Next.

  • This screen prompts for project workspace location. Make sure “Use default Workspace location” and “Create a simple project (skip archetype selection)” are selected and click Next.

  • Now we have to configure the project by entering the following details and click Finish.
    • Enter Group Id: as “com.theopentutorials.ejb3.client
    • Enter Artifact Id as “ejbclientmavendemo
    • Enter Version: as “1.0-SNAPSHOT
    • Packaging: jar
    • Name: EJB3 Client Maven

Create EJB3 client

  1. Create a new Java class “ClientUtility” with package “com.theopentutorials.ejb3” under “src/main/java
  2. Copy paste the following code.
    package com.theopentutorials.ejb3;
    import java.util.Properties;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    public class ClientUtility {
    	private static Context initialContext;
    	private static final String PKG_INTERFACES = "org.jboss.ejb.client.naming";
    	public static Context getInitialContext() throws NamingException {
    		if (initialContext == null) {
    			Properties properties = new Properties();
    			properties.put(Context.URL_PKG_PREFIXES, PKG_INTERFACES);
    			initialContext = new InitialContext(properties);
    		return initialContext;
  3. Now create another Java class “Client” in the same package and copy the following code.
    package com.theopentutorials.ejb3;
    import javax.naming.Context;
    import javax.naming.NamingException;
    import com.theopentutorials.ejb3.HelloWorld;
    public class Client {
    	public static void main(String[] args) {
    		HelloWorld bean = doLookup();
    		System.out.println(bean.sayHello()); // 4. Call business logic
    	private static HelloWorld doLookup() {
    		Context context = null;
    		HelloWorld bean = null;
    		try {
    			// 1. Obtaining Context
    			context = ClientUtility.getInitialContext();
    			// 2. Generate JNDI Lookup name
    			String lookupName = getLookupName();
    			// 3. Lookup and cast
    			bean = (HelloWorld) context.lookup(lookupName);
    		} catch (NamingException e) {
    		return bean;
    	private static String getLookupName() {
    		 * The app name is the EAR name of the deployed EJB without .ear suffix.
    		 * Since we haven't deployed the application as a .ear, the app name for
    		 * us will be an empty string
    		String appName = "";
    		 * The module name is the JAR name of the deployed EJB without the .jar
    		 * suffix.
    		String moduleName = "ejbmavendemo-1.0-SNAPSHOT";
    		 * AS7 allows each deployment to have an (optional) distinct name. This
    		 * can be an empty string if distinct name is not specified.
    		String distinctName = "";
    		// The EJB bean implementation class name
    		String beanName = "HelloWorldBean";
    		// Fully qualified remote interface name
    		final String interfaceName = "com.theopentutorials.ejb3.HelloWorld";
    		// Create a look up string name
    		String name = "ejb:" + appName + "/" + moduleName + "/" + distinctName
    				+ "/" + beanName + "!" + interfaceName;
    		return name;

EJB3 client pom.xml

Copy paste the following in your pom.xml file.

<?xml version="1.0"?>
<project xmlns="" xmlns:xsi=""
	<name>EJB3 Client Maven</name>
		<!-- Explicitly declaring the source encoding eliminates the following 
			message: -->
		<!-- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered 
			resources, i.e. build is platform dependent! -->

		<!-- JBoss dependency versions -->

		<!-- other plugin versions -->
		<!-- maven-compiler-plugin -->

			<!-- Define the version of JBoss' Java EE 6 APIs we want to use -->
			<!-- JBoss distributes a complete set of Java EE 6 APIs including a Bill 
				of Materials (BOM). A BOM specifies the versions of a "stack" (or a collection) 
				of artifacts. We use this here so that we always get the correct versions 
				of artifacts. Here we use the jboss-javaee-6.0 stack (you can read this as 
				the JBoss stack of the Java EE 6 APIs). You can actually use this stack with 
				any version of JBoss AS that implements Java EE 6, not just JBoss AS 7! -->



		<!-- Import the transaction spec API, we use runtime scope because we aren't 
			using any direct reference to the spec API in our client code -->

		<!-- Import the EJB 3.1 API, we use runtime scope because we aren't using 
			any direct reference to EJB spec API in our client code -->

		<!-- We depend on the EJB remote business interfaces of this application -->

		<!-- JBoss EJB client API jar. We use runtime scope because the EJB client 
			API isn't directly used in this example. We just need it in our runtime classpath -->

		<!-- client communications with the server use XNIO -->


		<!-- The client needs JBoss remoting to access the server -->

		<!-- Remote EJB accesses can be secured -->

		<!-- data serialization for invoking remote EJBs -->


			<!-- Enforce Java 1.6 -->

			<!-- Add the maven exec plugin to allow us to run a java program via maven -->
						<!-- automatically creates the classpath using all project dependencies, 
							also adding the project build directory -->

Maven Plugins used

1. maven-compiler-plugin
The maven-compiler-plugin is used to compile the sources of your project. We specified jdk 1.6 version for source and target classes.

2. exec-maven-plugin
Since our ejb client is a Java program, to run it we use the exec-maven-plugin which helps to execute system and Java programs. We need to specify the executable (i.e. java), classpath, and the java class (com.theopentutorials.ejb3.Client). The classpath is left empty because the plugin includes the necessary classpath arguments based on the dependencies provided.

Maven Dependencies for EJB3 client

In order to run EJB3 client we need to include the following dependencies,

  • We depend on the EJB remote business interfaces of this application to run the client. So we need to specify the ejb client jar dependency. The <type> tag with value “ejb-client” is used to specify this project’s dependency on the EJB client jar.
  • The dependencies jboss-transaction-api_1.1_spec, jboss-ejb-api_3.1_spec, jboss-ejb-client, xnio-api, xnio-nio, jboss-remoting, jboss-sasl, jboss-marshalling-river have scope as runtime because these are all not required for compilation.
  • The dependencies jboss-javaee-6.0 and jboss-as-ejb-client-bom under dependencyManagement have scope as import which is used to include dependency management information from a remote POM into the current project. These remote POMs are provided by JBoss which contains the necessary dependencies for running the client.

JBoss EJB Client properties

  • Create a file under “src/main/resources” and name it as “”
  • Copy paste the following code.

    remote.connection.default.port = 4447

Project Structure

Run the project

  • If the project shows any error update the project by right clicking the project, Maven -> Update Project
  • Then go to your pom.xml, right-click, select Run As -> Maven build
  • Specify the goal as exec:exec (provided by exec-maven-plugin)


If there are no errors, you should see the following output.

Hello World !!!

Tags: , , , , , , , , , , ,

  • Dhagej


    I tried the example step by step as you’ve mentioned, however when I run this I get following error:-

    javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(
    at javax.naming.InitialContext.getDefaultInitCtx(
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(
    at javax.naming.InitialContext.lookup(
    at com.ism.ejb3.Client.doLookup(
    at com.ism.ejb3.Client.main(
    Exception in thread "main" java.lang.NullPointerException
    at com.ism.ejb3.Client.main(

    • Nithya Vasudevan

      Please follow part 1 of this article where you create EJB and deploy it in server.

  • sam

    Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project ejbmavendemo: The parameters ‘executable’ for goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec are missing or invalid -> [Help 1]

  • Rafael


    I have a question. Where can I find a overvier from all jboss provided plugins?
    And where can I get a explanation about the plugins what they do and which goals
    I can use and what they do??

  • Ramzi

    Thank you.. very good examples

  • Ido

    Great tutorial, but in the “Client” class in “getLookupName” method the variable “interfaceName” is wrong. It says “ejbmavendemo-1.0-SNAPSHOT”, when the correct text is “ejbmavendemo-0.0.1-SNAPSHOT”.

    PS: Sorry my english.

  • deep2180

    The lookup name is wrong .. the correct format should be

    String appName = “”;
    String moduleName = “ejbmavendemo”;
    String distinctName = “”;
    String beanName = “HelloWorldBean”;
    final String interfaceName = “com.theopentutorials.ejb3.HelloWorld”;
    String name = “ejb:” + appName + “/” + moduleName + “/” + distinctName
    + “/” + beanName + “!” + interfaceName;

    When you start JBoss and load the bean the JNDI name is printed out..


    Take the first string


    The JNDI name represents
    In this case its
    String appName = “”;
    String moduleName = “ejbmavendemo”;
    String distinctName = “”;
    String beanName = “HelloWorldBean”;
    final String interfaceName = “com.theopentutorials.ejb3.HelloWorld”;


  • Trancendental

    Can anyone help – I get
    javax.naming.NamingException: Could not load ejb proxy class com.theopentutorials.ejb3.HelloWorld [Root exception is java.lang.ClassNotFoundException: com.theopentutorials.ejb3.HelloWorld]

    Client doesn’t even seem to be hitting the server

  • Andres Fernando Chalarca Lopez

    Excelent tutorial, thank’s so much.