erasmus77
erasmus77

Reputation: 327

Remote EJB Client Eclipse

I've been trying for some time to learn Java EE but I could never run an EJB example. Oracle's guide uses netbeans but I must learn how to do it in Eclipse. Neither did books did any help or youtube videos.

I can run servlets, jsp, jsf without problems but I always had problems with EJBs. What am I missing?

The problem is configuration within Eclipse I think. My Project Structure in Eclipse is the following:

My Project Structure in Eclipse is the following

The code of HelloWorld.java file:

package helloworld.ejb;
import javax.ejb.Remote;

@Remote
public interface HelloWorld {
    public String outputHelloWorld();
}

Code of the HelloWorldBean.java file

package helloworld.ejb;
import javax.ejb.Stateless;

@Stateless
public class HelloWorldBean implements HelloWorld {
    public String outputHelloWorld() {
    return "Hello World!";
    }
}

Code of the HelloWorldClient.java

package helloworldprojectclient;

import javax.ejb.EJB;
import helloworld.ejb.HelloWorld;

public class HelloWorldClient {
@EJB
private static HelloWorld helloWorld;
public static void main (String[] args) {
    System.out.println(helloWorld.outputHelloWorld());
    }
}

I am using Glassfish 4.0 as a server. The HelloWorldProject is an "EJB Project" while "helloworldprojectclient" is a regular Java Project and i've added javaee.jar (from the glassfish directory) to the buildpath.

When I try to run the HelloWorldClient.java I get the following exception:

Exception in thread "main" java.lang.NullPointerException at helloworldprojectclient.HelloWorldClient.main(HelloWorldClient.java:10) which is the following line: System.out.println(helloWorld.outputHelloWorld());

What is the problem? I mention i'm a total beginner at EJBs. Thank you!

Upvotes: 0

Views: 1968

Answers (2)

Gabriel Aramburu
Gabriel Aramburu

Reputation: 2981

Just in case you are still intrested in this:

The first version doesn´t work because you are trying to inject an ejb reference in a class that is not managed by a Container. When you execute the main method, the @EJB annotation is ignored, thus 'HelloWorld' class member is never initialized.

In order to execute this code without modification, you need execute the class in a Application Client Container that will inject the ejb reference.

Your second version runs because instead of to delegate to Container, you are getting the ejb reference through the JNDI service. This is the suggested way when Container injection is no available.

Upvotes: 2

erasmus77
erasmus77

Reputation: 327

I've managed to make it work. I don't know if this is the correct way but in the "helloworldprojectclient" if you set the buildpath's Project tab and add HelloWorldProject then on the Libraries tab add appserv-rt.jar and javaee.jar (both from glassfish lib folder) then the client should look like this:

package helloworldprojectclient;
import javax.naming.InitialContext;
import helloworld.ejb.HelloWorld;

public class HelloWorldClient {

public static void main(String[] args) {
    try {
        InitialContext ic = new InitialContext();
        HelloWorld thing = (HelloWorld) ic.lookup("helloworld.ejb.HelloWorld");
        System.out.println("It seems it runs: " + thing.outputHelloWorld());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

Upvotes: 0

Related Questions