Writing A Standalone Java Test Client

  1. In the Package Explorer View make sure the Calculator project is selected.

  2. Create a new Java class. Open menu File > New > Other... and choose Java > Class.

    Java > Class

  3. The package will be by.iba.client and the class name CalculatorClient.

    Leave the default options selected and be sure that public static void main(...) method is checked.

    Click the Finish button.

    CalculatorClient

  4. Add the following code to the public static void main method body:

    try {
    	Context jndiContext = new InitialContext();
    	Object ref = jndiContext.lookup("StatelessCalculatorBean/remote");
    	StatelessCalculator calc = (StatelessCalculator) PortableRemoteObject
    			.narrow(ref, StatelessCalculator.class);
    
    	System.out.println("4 + 3 = " + calc.add(4,3));
    	System.out.println("4 - 3 = " + calc.subtract(4,3));
    	System.out.println("4 * 3 = " + calc.multiply(4,3));
    	System.out.println("4 / 3 = " + calc.divide(4,3));
    			
    } catch (NamingException ne) {
    	ne.printStackTrace();
    }
    								

    NOTE: To resolve imports problems press Shift + Ctrl + O. Press Ctrl + S to save the Java class.

  5. Run the Java class by selecting menu Run > Run As > Java Application.

    NOTE: Make sure the CalculatorClient.java is selected in Package Explorer View.

    You should see the following result in the Console View:

    4 + 3 = 7.0
    4 - 3 = 1.0
    4 * 3 = 12.0
    4 / 3 = 1.3333333333333333
    								

Congratulations ! You have succesfully created and tested your first EJB 3.0 application !!!

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine