Error javax.naming.NoInitialContextException - console application

2

I'm trying to make a simple EJB, I followed some tutorials, I looked at the Wildfly sample code, and even then, I'm facing a mistake.

Could anyone help me?

The error:

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)

The Client Project Build Path:

MyEJB:

Thestructureofmyproject:

TheWildflyLog:

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

ClientAdder.properties (is in the classpath):

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);
}
    
asked by anonymous 02.02.2018 / 21:51

1 answer

1

I found the problem, incidentally, the problems:

Ideally, Wildfly designs should be with Maven to make it easier.

So I converted them to Maven.

Secondly, the properties file must necessarily have the name: jboss-ejb-client.properties

When converting the client to Maven, these dependencies and plugins are required:

   <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.AdditionEJBClient</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.AdditionEJBClient</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>


        </plugins>

    </build>

If you do not convert to Maven, just download this jar client from Jboss Wildfly and put it in the classpath:

link

    
03.02.2018 / 00:27