jndi查找API时的java.lang.NoClassDefFoundError

时间:2018-02-24 17:46:11

标签: java maven glassfish ejb noclassdeffounderror

当我在EJB中为API执行jndi查找时,我遇到了NoClassDefFoundError异常。这是我的项目的样子:

java project

在我的项目中,我有2个API,2个EJB和2个客户端。我的客户都在工作。我想要做的是通过调用ejb-31-entity-bean-exemple-api来创建EJB ejb-admin-bean函数ejb-31-entity-bean-exemple-bean。

以下是代码:

package admin.entity_bean_ejb;
import javax.ejb.Stateless;
import javax.naming.InitialContext;
import admin.entity_bean_api.StatelessSessionAdmin;
import enterprise.entity_bean_api.StatelessSessionLocal;
/**
 * The stateless session bean.
 */
@Stateless
public class StatelessSessionAdminBean implements StatelessSessionAdmin {

    @Override
    public String helloWorld() {
        return "the room is called " + getShowRoomName();       
    }

    //test d'appel de l'api d'encheres
    public String getShowRoomName() {
        StatelessSessionLocal sb;       

        try {       
            InitialContext ic = new InitialContext();           

               sb = (StatelessSessionLocal) ic.lookup("java:global/ejb-31-entity-bean-example-bean/StatelessSessionBean!enterprise.entity_bean_api.StatelessSessionLocal");

            final String roomName = sb.getShowRoomName(1);
            return roomName;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

未找到的类是来自ejb-31-entity-bean-example-api的StatelessSessionLocal

这是stackTrace:

javax.ejb.EJBException异常     at com.sun.ejb.containers.EJBContainerTransactionManager.processSystemException(EJBContainerTransactionManager.java:752)

引起:java.lang.ClassNotFoundException:enterprise.entity_bean_api.StatelessSessionLocal     在com.sun.enterprise.loader.ASURLClassLoader.findClassData(ASURLClassLoader.java:865)     在com.sun.enterprise.loader.ASURLClassLoader.findClass(ASURLClassLoader.java:742)     at java.lang.ClassLoader.loadClass(ClassLoader.java:424)     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)     ... 52更多

我使用Eclipse,glassfish和maven。以下是我项目的POM: 这是父母pom:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.glassfish-samples</groupId>
  <artifactId>ejb-31-entity-bean-example</artifactId>
  <packaging>pom</packaging>
  <name>ejb-31-entity-bean-example</name>
  <version>4.0-SNAPSHOT</version>
  <!-- repositories for the checkstyle configuration TSP CSC -->
  <pluginRepositories>
    <pluginRepository>
      <id>tsp-csc-checkstyle-config-stable</id>
      <name>TSP CSC Checkstyle configuration, stable</name>
      <url>http://www-inf.telecom-sudparis.eu/COURS/CSC4102/maven-repository/stable/</url>
    </pluginRepository>
    <pluginRepository>
      <id>tsp-csc-checkstyle-config-snapshot</id>
      <name>TSP CSC Checkstyle configuration, snapshot</name>
      <url>http://www-inf.telecom-sudparis.eu/COURS/CSC4102/maven-repository/snapshot/</url>
    </pluginRepository>
  </pluginRepositories>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <glassfish.version>4.1</glassfish.version>
  </properties>
  <build>
    <plugins>
      <!-- maven-eclipse configuration -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <version>2.10</version>
        <configuration>
          <downloadSources>true</downloadSources>
          <downloadJavadocs>true</downloadJavadocs>
        </configuration>
      </plugin>
      <!-- java compiler configuration -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <!-- surefire configuration -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          <enableAssertions>true</enableAssertions>
      <reuseForks>false</reuseForks>
        </configuration>
      </plugin>
      <!-- source packaging configuration -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>2.2.1</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
              <goal>test-jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <!-- javadoc packaging in jar configuration -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <version>2.9.1</version>
        <executions>
          <execution>
            <id>attach-javadocs</id>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
            <configuration>
              <docfilessubdirs>true</docfilessubdirs>
              <detectOfflineLinks>false</detectOfflineLinks>
              <detectJavaApiLink>false</detectJavaApiLink>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <!-- checkstyle configuration -->
      <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>2.17</version>
    <executions>
      <execution>
            <goals>
              <goal>check</goal>
            </goals>
      </execution>
    </executions>
    <dependencies>
          <dependency>
            <groupId>eu.telecomsudparis.csc</groupId>
            <artifactId>tsp-csc-config-checkstyle</artifactId>
            <version>1.1-SNAPSHOT</version>
          </dependency>
    </dependencies>
    <configuration>
      <checkstyle.config.location>config/checkstyle/sun_checks_adapted_to_tsp_csc.xml</checkstyle.config.location>
      <configLocation>config/checkstyle/sun_checks_adapted_to_tsp_csc.xml</configLocation>
      <encoding>UTF-8</encoding>
      <consoleOutput>true</consoleOutput>
      <failsOnError>false</failsOnError>
      <linkXRef>false</linkXRef>
    </configuration>
      </plugin>
      <!-- site configuration -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.6</version>
        <configuration>
          <locales>fr</locales>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <!-- reports in the maven site -->
  <reporting>
    <plugins>
      <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-project-info-reports-plugin</artifactId>
    <version>2.4</version>
    <configuration>
          <dependencyDetailsEnabled>false</dependencyDetailsEnabled>
          <dependencyLocationsEnabled>false</dependencyLocationsEnabled>
    </configuration>
      </plugin>
      <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <version>2.9.1</version>
      </plugin>
      <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>findbugs-maven-plugin</artifactId>
    <version>3.0.4</version>
      </plugin>
      <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>2.17</version>
    <configuration>
      <configLocation>config/checkstyle/sun_checks_adapted_to_tsp_csc.xml</configLocation>
      <encoding>UTF-8</encoding>
      <consoleOutput>true</consoleOutput>
      <failsOnError>false</failsOnError>
      <linkXRef>false</linkXRef>
    </configuration>
      </plugin>
    </plugins>
  </reporting>
  <modules>
    <module>ejb-31-entity-bean-example-entity</module>
    <module>ejb-31-entity-bean-example-api</module>
    <module>ejb-31-entity-bean-example-bean</module>
    <module>ejb-31-entity-bean-example-client</module>
    <module>ejb-admin-bean</module>
    <module>ejb-admin-api</module>
    <module>ejb-admin-entity</module>
    <module>ejb-admin-client</module>
  </modules>
</project>

以下是想要调用api的bean的POM:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.glassfish-samples</groupId>
    <artifactId>ejb-31-entity-bean-example</artifactId>
    <version>4.0-SNAPSHOT</version>
  </parent>
  <groupId>org.glassfish-samples.entity-bean-example2</groupId>
  <artifactId>ejb-admin-bean</artifactId>
  <packaging>ejb</packaging>
  <name>The EJB 3.2 Entity Bean Sample Application</name>
  <version>4.0-SNAPSHOT</version>

  <dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-shade-plugin -->
<!-- <dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.1.0</version>
</dependency>
-->
    <dependency>
      <groupId>org.glassfish-samples.entity-bean-example2</groupId>
      <artifactId>ejb-admin-entity</artifactId>
      <version>4.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish-samples.entity-bean-example2</groupId>
      <artifactId>ejb-admin-api</artifactId>
      <version>4.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>7.0</version>
    </dependency>        
    <!-- embedded glassfish for JUnit tests -->
    <dependency>
      <groupId>org.glassfish.extras</groupId>
      <artifactId>glassfish-embedded-all</artifactId>
      <version>3.1.1</version>
      <scope>test</scope>
    </dependency>
    <!-- JUnit tests -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
<!--interaction entre les modules de l'appli. ajout perso-->
<dependency>
 <groupId>org.glassfish-samples.entity-bean-example</groupId>
      <artifactId>ejb-31-entity-bean-example-api</artifactId>
      <version>4.0-SNAPSHOT</version>   
    </dependency>
  </dependencies>

  <build>
    <finalName>entity-bean2</finalName>
    <plugins>
      <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.0.1</version>
    <executions>
      <execution>
        <id>unpack</id>
        <phase>process-classes</phase>
        <goals>
          <goal>unpack</goal>
        </goals>
        <configuration>
          <artifactItems>
                <artifactItem>
          <groupId>org.glassfish-samples.entity-bean-example2</groupId>
          <artifactId>ejb-admin-entity</artifactId>
          <outputDirectory>${project.build.outputDirectory}</outputDirectory>
        </artifactItem>
                <artifactItem>
          <groupId>org.glassfish-samples.entity-bean-example2</groupId>
          <artifactId>ejb-admin-api</artifactId>
          <outputDirectory>${project.build.outputDirectory}</outputDirectory>
        </artifactItem>
          </artifactItems>
        </configuration>
      </execution>
    </executions>
      </plugin>
      <!-- build the ejb jar -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-ejb-plugin</artifactId>
    <version>2.5.1</version>
        <configuration>
          <jarName>entity-bean2</jarName>
          <ejbVersion>3.2</ejbVersion>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

感谢您的帮助!

0 个答案:

没有答案