错误javax.naming.NoInitialContextException - 控制台应用程序

时间:2018-02-02 15:05:41

标签: java ejb wildfly

我试图做一个简单的ejb,我遵循了许多教程,但是,我的项目中有一些错误,我不知道为什么我错了。

拜托,请有人帮帮我吗?

错误:

ejb:/Adder//Addition!com.labs.AdditionRemote
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(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at com.labs.StartCalc.main(StartCalc.java:25)

客户项目的构建路径:

enter image description here

我的Ejb:

enter image description here

我的项目结构:

enter image description here

WildFly的日志:

 WFLYWELD0003: Processing weld deployment Adder.jar
JNDI bindings for session bean named Addition in deployment unit deployment "Adder.jar" are as follows:
java:global/Adder/Addition!com.labs.Addition
java:app/Adder/Addition!com.labs.Addition
java:module/Addition!com.labs.Addition
java:global/Adder/Addition!com.labs.AdditionRemote
java:app/Adder/Addition!com.labs.AdditionRemote
java:module/Addition!com.labs.AdditionRemote
java:jboss/exported/Adder/Addition!com.labs.AdditionRemote

AdderClient.properties(我在这里打破了最后一行)

endpoint.name=client-endpoint
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=localhost
remote.connection.default.port = 8080
remote.connection.default.connect.
options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
remote.connection.default.username=ejbuser
remote.connection.default.password=EjbUser!1

AdderClient

package com.labs;
...
public class StartCalc {
public static void main(String[] args) {           
    try{   
        Properties props = new Properties();
        props.put(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming");

        InitialContext context = new InitialContext(props);

        String appName = "";             
        String moduleName = "Adder";
        String distinctName = "";            
        String beanName = Addition.class.getSimpleName();            
        String interfaceName = AdditionRemote.class.getName();
        String name = "ejb:" + appName + "/" + moduleName + "/" +  distinctName    + "/" + beanName + "!" + interfaceName;

        AdditionRemote bean = (AdditionRemote)context.lookup(name);

Adder.jar

package com.labs;
...
@Stateless
@LocalBean
public class Addition implements AdditionRemote {
public Addition() {
}
public int add(int a,int b){
    int r=a+b;
    return r;
}
}

package com.labs;
import javax.ejb.Remote;
@Remote
public interface AdditionRemote {
   public int add(int a , int b);
}

1 个答案:

答案 0 :(得分:0)

我发现了问题:

1 - 使用Maven创建项目很有用 比我转换它们。

2 - 属性文件必须使用名称 jboss-ejb-client.properties (某些教程误导我)

仅使用Eclipse(无Maven)的客户端项目

如果您想使用没有Maven的客户端,您需要下载Wildfly的jar客户端,可在此处找到:https://mvnrepository.com/artifact/org.wildfly/wildfly-client-all

new project using wildflys client jar

使用Maven的客户端项目

now my client using maven

将客户端项目转换为Maven时,必须使用以下依赖项和插件:

<dependencyManagement>
      <dependencies>

         <dependency>
            <groupId>org.jboss.spec</groupId>
            <artifactId>jboss-javaee-7.0</artifactId>
            <version>1.0.3.Final</version>
            <type>pom</type>
            <scope>import</scope>
         </dependency>

         <dependency>
             <groupId>org.wildfly</groupId>
             <artifactId>wildfly-ejb-client-bom</artifactId>
             <version>10.1.0.Final</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>

      </dependencies>

    </dependencyManagement>

    <dependencies>

        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.jboss.spec.javax.transaction</groupId>
            <artifactId>jboss-transaction-api_1.2_spec</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- Import the EJB API, we use runtime scope because we aren't using
            any direct reference to EJB spec API in our client code -->
        <dependency>
            <groupId>org.jboss.spec.javax.ejb</groupId>
            <artifactId>jboss-ejb-api_3.2_spec</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- Include the ejb client jars -->
        <dependency>
            <groupId>org.wildfly</groupId>
            <artifactId>wildfly-ejb-client-bom</artifactId>
            <type>pom</type>
            <scope>runtime</scope>
        </dependency>

    </dependencies>

  <build>
    <finalName>${project.artifactId}</finalName>
    <plugins>

            <!-- Add the maven exec plug-in to allow us to run a java program
                via maven -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>${version.exec.plugin}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>java</executable>
                    <workingDirectory>${project.build.directory}/exec-working-directory</workingDirectory>
                    <arguments>
                        <!-- automatically creates the classpath using all
                            project dependencies, also adding the project build directory -->
                        <argument>-classpath</argument>
                        <classpath>
                        </classpath>
                        <argument>com.labs.StartCalc</argument>
                    </arguments>
                </configuration>
            </plugin>
            <!-- build standalone exe jar -->   
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.labs.StartCalc</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>


        </plugins>

    </build>