0

I'd like to use JBoss Weld as a CDI facility in a classic Hello World application in a maven structured project. To keep the things as clean & simple as possible, I only created a Weld environment object and nothing more. I also created the deployment descriptor file beans.xml in src/main/resources/META-INF and src/test/resources directories. I did't go further by creating and initializing a WeldContainer etc. I'm just wondering why this settings don't work in the first place.

The application compiles perfectly and generates an executable jar file with mvn package command. However I got a runtime error:

C:\dev\eclipse-workspace\my-app>java -cp target/my-app-1.0.jar app.Hello
Exception in thread "main" java.lang.NoClassDefFoundError: org/jboss/weld/environment/se/Weld
        at app.Hello.main(Hello.java:7)
Caused by: java.lang.ClassNotFoundException: org.jboss.weld.environment.se.Weld
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.j
ava:602)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoader
s.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        ... 1 more

So, here is my main class:

 1 package app;
 2
 3 import org.jboss.weld.environment.se.Weld;
 4
 5 public class Hello {
 6     public static void main(String[] args) {
 7         Weld weld = new Weld();
 8          
 9         System.out.println("Hello World");
10        
11         weld.shutdown();
12         System.exit(0);
13     }
14 }

my pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<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
            https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>jee</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0</version>
  <name>my-app</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>13</maven.compiler.source>
    <maven.compiler.target>13</maven.compiler.target>
  </properties>

  <dependencies>

    <dependency>
      <groupId>org.jboss.weld.se</groupId>
      <artifactId>weld-se-core</artifactId>
      <version>3.1.3.Final</version>
    </dependency>

  </dependencies>

</project>

and lastly my project structure:

enter image description here

Your help is appreciated in advance.

Thank you.

Alex

alix
  • 31
  • 2

1 Answers1

0

You will note that java -cp target/my-app-1.0.jar app.Hello does not include the Weld jars on the classpath (unless your jar file's META-INF/MANIFEST.MF has a Class-Path entry that references them). That is why Weld classes cannot be found at runtime when you start your application in this manner.

Laird Nelson
  • 15,321
  • 19
  • 73
  • 127