Hot questions for Using Enterprise JavaBeans in eclipse

Question:

I have installed Eclipse, WildFly and Jboss Tools. I created an EAR/Maven project and an EJB/Maven project.

I added all the dependencies to the EAR pom.xml. Jboss Tools deploys the WildFly EAR lib with no issues and the application runs fine.

The problem is the build classpath in the EJB project in Eclipse. As all the dependencies are configured in the EAR pom.xml and not in the EJB pom.xml, I cannot figure out how to tell the EJB project in Eclipse to take from the EAR project the jars that were defined as dependencies.

How to set this up?


Answer:

Add the dependencies in EJB pom.xml and not in EAR pom.xml.

Question:

I have created a simple EJB 3.1 project using eclipse IDE, and below is the layout of the project:

I created a simple Session Bean (stateless) and the above is the directory structure.

I have read books (to the extend needed) on EJB 3.x, but no where there is any mention of all this directory structure.

Can anyone help me understand the logical relation of these directory structure and how can I learn it in an easy-to-understand without cramming the things.


Answer:

What you get with Eclipse is the structure that suits Eclipse. If you create the same with Netbeans or IntelliJ you will likely get another directory structure, or if you use Maven you get another (customizable) structure.

What is important is the final build. If you export your project in Eclipse you will get the standard deployable structure conform to JEE specification.

To reassume, each vendor (Eclipse, Netbeans...etc) has its own way to arrange things and it is detailed in the corresponding manual. But every vendor must adhere to specifications when producing the final build.

Then few things about your screenshot:

FirstEJBProj

where your EJBs reside, ejbModule is where Eclipse keep source code (Java file).

FirstEJBProjClient

here you will find the business interfaces implemented by the EJBs, that you can pack and distribute to your client, remote or local. Again ejbModule is where all your classes are. Also, I don't know why Eclipse creates it as an EJB project, a Jar would be enough.

FirstEJBProjEar

Just the ear container that gather all.

Am I answered your question ?

Question:

I am a newcomer in Maven and I want to import my application to maven. I want to, as before, refer on classes in my ejb-project into my war-project. I have no errors in classes, but if I make maven install on my Parent project then my war-project give me this error:

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-compile) on project SportunionWARModule: Compilation failure: Compilation failure:

[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/AdministratorenView.java:[6,37] package com.sportunion.project.domain does not exist
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/AdministratorenView.java:[7,37] package com.sportunion.project.domain does not exist
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/AdminEditor.java:[7,37] package com.sportunion.project.domain does not exist
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/AdministratorenView.java:[41,30] cannot find symbol
[ERROR] symbol:   class Admins
[ERROR] location: class com.sportunion.AdministratorenView
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/AdministratorenView.java:[42,30] cannot find symbol
[ERROR] symbol:   class Log
[ERROR] location: class com.sportunion.AdministratorenView
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/CSVLoader.java:[8,30] package com.sportunion.project does not exist
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/CSVLoader.java:[9,36] package com.sportunion.project.utils does not exist
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/CSVLoader.java:[13,9] cannot find symbol
[ERROR] symbol:   class UsersService
[ERROR] location: class com.sportunion.CSVLoader
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/BenutzerView.java:[9,37] package com.sportunion.project.domain does not exist
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/BenutzerView.java:[53,30] cannot find symbol
[ERROR] symbol:   class Users
[ERROR] location: class com.sportunion.BenutzerView
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/Ranking.java:[8,37] package com.sportunion.project.domain does not exist
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/Ranking.java:[23,30] cannot find symbol
[ERROR] symbol:   class Users
[ERROR] location: class com.sportunion.Ranking
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/MyUI.java:[8,37] package com.sportunion.project.domain does not exist
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/MyUI.java:[9,37] package com.sportunion.project.domain does not exist
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/MyUI.java:[10,37] package com.sportunion.project.domain does not exist
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/MyUI.java:[35,25] cannot find symbol
[ERROR] symbol:   class Admins
[ERROR] location: class com.sportunion.MyUI
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/MyUI.java:[36,29] cannot find symbol
[ERROR] symbol:   class Users
[ERROR] location: class com.sportunion.MyUI
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/MyUI.java:[37,29] cannot find symbol
[ERROR] symbol:   class Users
[ERROR] location: class com.sportunion.MyUI
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/MyUI.java:[38,29] cannot find symbol
[ERROR] symbol:   class Log
[ERROR] location: class com.sportunion.MyUI
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/LogView.java:[7,37] package com.sportunion.project.domain does not exist
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/LogView.java:[30,30] cannot find symbol
[ERROR] symbol:   class Log
[ERROR] location: class com.sportunion.LogView
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/LoginView.java:[8,30] package com.sportunion.project does not exist
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/LoginView.java:[9,37] package com.sportunion.project.domain does not exist
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/LoginView.java:[10,30] package com.sportunion.project does not exist
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/LoginView.java:[11,36] package com.sportunion.project.utils does not exist
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/AdministratorenView.java:[101,32] cannot find symbol
[ERROR] symbol: class Admins
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/AdministratorenView.java:[101,68] cannot find symbol
[ERROR] symbol: class Admins
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/AdministratorenView.java:[101,80] cannot find symbol
[ERROR] symbol: class Admins
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/CSVLoader.java:[13,30] cannot find symbol
[ERROR] symbol:   variable PTServiceLocator
[ERROR] location: class com.sportunion.CSVLoader
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/BenutzerView.java:[118,40] cannot find symbol
[ERROR] symbol: class Users
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/BenutzerView.java:[118,75] cannot find symbol
[ERROR] symbol: class Users
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/BenutzerView.java:[118,86] cannot find symbol
[ERROR] symbol: class Users
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/MyUI.java:[66,64] cannot find symbol
[ERROR] symbol:   class Admins
[ERROR] location: class com.sportunion.MyUI
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/MyUI.java:[67,63] cannot find symbol
[ERROR] symbol:   class Users
[ERROR] location: class com.sportunion.MyUI
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/MyUI.java:[68,65] cannot find symbol
[ERROR] symbol:   class Users
[ERROR] location: class com.sportunion.MyUI
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/MyUI.java:[69,61] cannot find symbol
[ERROR] symbol:   class Log
[ERROR] location: class com.sportunion.MyUI
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/LoginView.java:[41,13] cannot find symbol
[ERROR] symbol:   class AdminsService
[ERROR] location: class com.sportunion.LoginView
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/LoginView.java:[41,36] cannot find symbol
[ERROR] symbol:   variable PTServiceLocator
[ERROR] location: class com.sportunion.LoginView
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/LoginView.java:[42,13] cannot find symbol
[ERROR] symbol:   class LogService
[ERROR] location: class com.sportunion.LoginView
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/LoginView.java:[42,30] cannot find symbol
[ERROR] symbol:   variable PTServiceLocator
[ERROR] location: class com.sportunion.LoginView
[ERROR] /C:/jboss/workspace/Sportunion_Maven/SportunionParent/SportunionWARModule/src/main/java/com/sportunion/LoginView.java:[58,17] cannot find symbol
[ERROR] symbol: class Admins

Does anyone now whats my mistake is?

this is my pom.xml of the war-Project:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
    <artifactId>SportunionParent</artifactId>
    <groupId>SportunionParent</groupId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>

<groupId>SportunionParent</groupId>
<artifactId>SportunionWARModule</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SportunionWARModule</name>

<properties>
    <vaadin.version>7.6.4</vaadin.version>
    <vaadin.plugin.version>${vaadin.version}</vaadin.plugin.version>
    <jetty.plugin.version>9.2.3.v20140905</jetty.plugin.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<repositories>
    <repository>
        <id>vaadin-addons</id>
        <url>http://maven.vaadin.com/vaadin-addons</url>
    </repository>
    <repository>
        <id>vaadin-snapshots</id>
        <url>https://oss.sonatype.org/content/repositories/vaadin-snapshots/</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
        </snapshots>
    </repository>
</repositories>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-bom</artifactId>
            <version>${vaadin.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-server</artifactId>
    </dependency>
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-push</artifactId>
    </dependency>
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-client</artifactId>
        <scope>provided</scope>
    </dependency>
    <!--
      Needed when using the widgetset optimizer (custom ConnectorBundleLoaderFactory).

      For widgetset compilation, vaadin-client-compiler is automatically added on the
      compilation classpath by vaadin-maven-plugin so normally there is no need for an
      explicit dependency.

    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-client-compiler</artifactId>
        <scope>provided</scope>
    </dependency>-->        
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-themes</artifactId>
    </dependency>
    <dependency>
        <groupId> com.vaadin.addon </groupId>
        <artifactId> jpacontainer </artifactId>
        <version> 3.1.1 </version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.opencsv</groupId>
        <artifactId>opencsv</artifactId>
        <version>3.3</version>
    </dependency>           
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <!-- Exclude some unnecessary files generated by the GWT compiler. -->
                <packagingExcludes>WEB-INF/classes/VAADIN/gwt-unitCache/**,
                    WEB-INF/classes/VAADIN/widgetsets/WEB-INF/**</packagingExcludes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-maven-plugin</artifactId>
            <version>${vaadin.plugin.version}</version>
            <configuration>
                <extraJvmArgs>-Xmx512M -Xss1024k</extraJvmArgs>
                <webappDirectory>${basedir}/target/classes/VAADIN/widgetsets</webappDirectory>
                <draftCompile>false</draftCompile>
                <compileReport>false</compileReport>
                <style>OBF</style>
                <strict>true</strict>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>update-theme</goal>
                        <goal>update-widgetset</goal>
                        <goal>compile</goal>
                        <!-- Comment out compile-theme goal to use on-the-fly theme compilation -->
                        <!-- <goal>compile-theme</goal> -->
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.4</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-clean-plugin</artifactId>
            <version>2.6.1</version>
            <!-- Clean up also any pre-compiled themes -->
            <configuration>
                <filesets>
                    <fileset>
                        <directory>src/main/webapp/VAADIN/themes</directory>
                        <includes>
                            <include>**/styles.css</include>
                            <include>**/styles.scss.cache</include>
                        </includes>
                    </fileset>
                </filesets>
            </configuration>
        </plugin>

        <!-- The Jetty plugin allows us to easily test the development build by
            running jetty:run on the command line. -->
        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>${jetty.plugin.version}</version>
            <configuration>
                <scanIntervalSeconds>2</scanIntervalSeconds>
            </configuration>
        </plugin>
    </plugins>
</build>

<profiles>
    <profile>
        <!-- Vaadin pre-release repositories -->
        <id>vaadin-prerelease</id>
        <activation>
        </activation>

        <repositories>
            <repository>
                <id>vaadin-prereleases</id>
                <url>http://maven.vaadin.com/vaadin-prereleases</url>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>vaadin-prereleases</id>
                <url>http://maven.vaadin.com/vaadin-prereleases</url>
            </pluginRepository>
        </pluginRepositories>
    </profile>
</profiles>

this is my pom.xml of my ejb-project:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
    <artifactId>SportunionParent</artifactId>
    <groupId>SportunionParent</groupId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>

<groupId>SportunionParent</groupId>
<artifactId>SportunionEJBModule</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>ejb</packaging>

<name>SportunionEJBModule</name>

<properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ejb-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <ejbVersion>3.1</ejbVersion>
                <archive>
                <manifest>
                    <addClasspath>true</addClasspath>

                    <!-- without this, the datetime stamp unique id's will be appended to classpath items -->
                    <!-- see: http://maven.apache.org/shared/maven-archiver/examples/classpath.html#Snapshot -->
                    <useUniqueVersions>false</useUniqueVersions>
                </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${endorsed.dir}</outputDirectory>
                        <silent>true</silent>
                        <artifactItems>
                            <artifactItem>
                                <groupId>javax</groupId>
                                <artifactId>javaee-endorsed-api</artifactId>
                                <version>6.0</version>
                                <type>jar</type>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

and this one from my ear-project:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>SportunionParent</groupId>
    <artifactId>SportunionParent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>SportunionEARModule</artifactId>
  <packaging>ear</packaging>
  <dependencies>
    <dependency>
        <groupId>SportunionParent</groupId>
        <artifactId>SportunionWARModule</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>war</type>
    </dependency>
    <dependency>
        <groupId>SportunionParent</groupId>
        <artifactId>SportunionEJBModule</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>ejb</type>
    </dependency>
  </dependencies>
  <build>
  <plugins>
    <plugin>
                <groupId>org.wildfly.plugins</groupId>
                <artifactId>wildfly-maven-plugin</artifactId>
                <version>1.0.2.Final</version>

                <configuration>
                        <hostname>127.0.0.1</hostname>
                        <port>9090</port>
                        <!-- <jbossHome>C:/tools/wildfly-8.0.0.Final</jbossHome> -->
                    </configuration>

    </plugin>
  </plugins>
  </build>
  <groupId>SportunionParent</groupId>
  <version>0.0.1-SNAPSHOT</version>
</project>

I would be happy if anyone could help me... Thanks in advice!


Answer:

As you mentioned, the missing elements are present on your EJB module.

Therefore, you need to remove your EJB module as a dependency from your EAR and add it to your WAR module. In your WAR's pom.xml file, add the following:

<dependencies>
    (...)

    <dependency>
        <groupId>SportunionParent</groupId>
        <artifactId>SportunionEJBModule</artifactId>
        <version>${project.version}</version>
    </dependency>

    (...)
</dependencies>

Also, since the EJB module is now a dependency of your WAR, I believe there's no need for you to have an EAR. Maven will place your EJB module on the WEB-INF/lib of your WAR.

Question:

I got an existing web project. Now I got a requirement to add a MDB EJB project and did the same. The EJB project maven depends on the web project

So all is well until now. The moment I start to use some of the classes in the web project in my MDB it compiles fine and everything look great But bombs in the runtime with NoClassDefFoundError

I went to my web pom.xml and added this in the configuration secstion

 <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <attachClasses>true</attachClasses>

and in my EJB POM

<dependency>
        <groupId>my.group</groupId>
        <artifactId>my-web-project-name</artifactId>
        <version>${project.version}</version>
        <classifier>classes</classifier>
    </dependency>

How to make my EJB find the classes inside the web project

I really can't do it the other way as i am not allowed to edit the web project. Please Help!!!


Answer:

Basically by default all servers use module classloaders and web module may depend on ejb, but not other way around. It is definitely not recommended. You have following options:

  • if you are on Java EE 6 compliant server, you could add your EJBs directly to web module (requires changes in the web project)
  • extract common classes from web project and create java utility project. Add that project to EAR, and make web and ejb project depend on that, and web depend on ejb (also requires changes in the web)
  • configure single classloader for your whole application. If your server allows that, this should solve your issues.

So to summarize, I'd rather recommend edit web project and use one of two first solutions, but if you really cant then third solution should work.

Question:

Look Below. How am I able to install EJB? I am using Java EE isn't it installed EJB by default?? I have screen shot the problem below


Answer:

You don't need to do anything special in Eclipse to create an EJB project, as from Eclipse's perspective an EJB is just a bunch of Java classes (POJO's) which get compiled into either a JAR file or a WAR file. It's what the you're Java EE Application Server does with the EJB that makes it special.

The EJB container which is part of the Java EE Application Server will scan the compiled Java classes and look for classes that have been annotated with either @Stateless or @Stateful; these classes will become EJBs. You can then inject the EJB into another class by annotating a field with @EJB.

As an example you can create an EJB called HelloWorldService using the following code:

@Stateless
public class HelloWorldService {

    public String sayHello() {
        String helloMessage = "Hello World";
        return helloMessage;
    }
}

You can then inject the HelloWorldServiceEJB into another class using the following code:

public class SomeOtherClass {

    @EJB
    private HelloWorldService helloWorldService;

    public void doSomething() {
        System.out.println(helloWorldService.sayHello());
    }
}

This is an oversimplified example and in reality you would never use an EJB for something this simple, but nevertheless it demonstrates the basics of creating an EJB and injecting it into another class.

I'd suggest looking for some tutorials on using EJBs or finding a book on the subject as the example I've given you was just to show that you don't need to do anything special in Eclipse.

Hope this helps.