Error javax.naming.Noinitialcontextexception - console application

Asked

Viewed 475 times

2

I’m trying to make a simple EJB, followed some tutorials, analyzed the Wildfly sample code, and yet, I’m facing an error.

Could someone help me?

The mistake:

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:

Build path

My EJB:

Meu EJB

The structure of my project:

A estrutura do meu projeto

The Log of the 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

Clientadder.properties (is in 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);
}
  • 1

    Rename ClientAdder.properties for jboss-ejb-client.properties and ensure that this file is inside the generated jar (e.g., if your project is Maven put the file inside the folder src/main/resources. Also ensure that jboss-client.jar is in the client’s classpath.

  • Thanks for the help, yes, those were two problems, another problem is that I noticed that there were missing wildfly libraries on the client side.

  • now that I’ve noticed, that’s right, just add this library jboss-client.jar and in the draft resolution, in my version: https://mvnrepository.com/artifact/org.wildfly/wildfly-client-all/9.0.1.Final

1 answer

1


I found the problem, by the way, the problems:

Ideal is that projects for Wildfly are with Maven to facilitate.

So I converted them to Maven.

Second, the property 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 don’t convert to Maven, just download this jar client from Jboss Wildfly and put it in the classpath:

https://mvnrepository.com/artifact/org.wildfly/wildfly-client-all

Browser other questions tagged

You are not signed in. Login or sign up in order to post.