Manoj Suthar
Manoj Suthar

Reputation: 1455

Unable to use Weld for Context and Dependency Injection (CDI) in my stand alone Java SE application

I am trying to make a standalone Java SE application with CDI. I followed this article and I am using Weld. However when I try to instantiate Weld weld = new Weld(); I am getting the following ClassNotFound error.

Exception in thread "main" java.lang.NoClassDefFoundError: org/jboss/weld/environment/ContainerInstanceFactory
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.agorava.socializer.Test.main(Test.java:59)
Caused by: java.lang.ClassNotFoundException: org.jboss.weld.environment.ContainerInstanceFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 13 more

I checked the weld-se JAR an it doesn't contain this Interface. What is the issue here? Thanks in advance.

Following is my Test class org.agorava.socializer.Test

public class Test  {

    public static void main(String[] args){

        Weld w = new Weld();
        WeldContainer factory = w.initialize();
        OAuthLifeCycleService service = factory.instance().select(OAuthLifeCycleService.class).get();
        System.out.println(service.getCurrentService());        

    }

}

Upvotes: 1

Views: 3228

Answers (2)

Youness
Youness

Reputation: 2070

I had this problem while writing unit tests. Here is the answer on github and here is the related part in my pom file:

    <!--Enable CDI in tomcat-->
    <dependency>
        <groupId>org.jboss.weld.servlet</groupId>
        <artifactId>weld-servlet</artifactId>
        <version>2.4.5.Final</version>
    </dependency>
    <!--Enables unit testing CDI apps-->
    <dependency>
        <groupId>org.jglue.cdi-unit</groupId>
        <artifactId>cdi-unit</artifactId>
        <version>4.0.1</version>
        <scope>test</scope>
    </dependency>
    <!--This is required implicitly for cdi-unit 4.0.1-->
    <dependency>
        <groupId>org.jboss.weld.se</groupId>
        <artifactId>weld-se</artifactId>
        <version>2.4.5.Final</version>
        <scope>test</scope>
    </dependency>

Upvotes: 0

anjava
anjava

Reputation: 63

Seems your problem is related to version "org.jboss.weld.environment.ContainerInstanceFactory" interface is available in newer version (Weld 3) refer https://github.com/weld/core/commit/36563d252a4a2375692d7669e7a523bb6ba89136

Try updating your version of weld-se.

Upvotes: 1

Related Questions