-1

实际上,我想使用 Weld-SE 创建一个类似于 JavaSE 应用程序的小型“Hello WOrld”示例,但似乎存在运行时错误。

这是我的课:

package de.mycompany.weldapp;

import java.util.List;

import javax.enterprise.event.Observes;
import javax.inject.Singleton;

import org.jboss.weld.environment.se.bindings.Parameters;
import org.jboss.weld.environment.se.events.ContainerInitialized;

@Singleton
public class App 
{
   public void printHello(@Observes ContainerInitialized event,     @Parameters List<String> parameters) {
       System.out.println("Hello " + parameters.get(0));
   }
}

这是我的 pom.xml:

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>de.mycompany</groupId>
<artifactId>weldapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>weldapp</name>
<url>http://maven.apache.org</url>

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
  <dependency>
    <groupId>org.jboss.weld.se</groupId>
    <artifactId>weld-se-shaded</artifactId>
    <version>3.0.2.Final</version>
  </dependency>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
  </dependency>
  <dependency>
    <groupId>javax.enterprise</groupId>
    <artifactId>cdi-api</artifactId>
    <version>2.0</version>
    <scope>provided</scope>
  </dependency>
</dependencies>

<build>
  <plugins>
    <plugin>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.0.2</version>
      <configuration>
        <archive>
          <manifest>
            <mainClass>de.mycompany.weldapp.App</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>
</project>

我已经安装了 Java JDK 1.8.0_151 和 Maven 3.5.2。编译过程成功,但是当我在目标目录中执行创建的 jar 文件时,我会一直收到以下错误:

A JNI error has occurred, plase check your installation and try again.
Exception in thread "main" java.lang.noClassDefFoundError: org/jboss/environment/se/events/ContainerInitialized
at java.lang.Class.getDeclaredMethods0(native Method)
... 
Caused by : java.lang.ClassNotFoundException: org.jboss.weld.environment.se.events.ContainerInitialized
at java.net.URLCLassLoader.findClass(Unknown Source)
...

有什么我没有提到或我做错了吗?是否有任何依赖性,不包括在内?

我再次安装了 Java、Maven 和 Eclipse,但没有任何帮助。

非常感谢丹尼尔

4

1 回答 1

0

由于您在 SE 模式下运行,因此您不想提供任何依赖项。所以<scope>provided</scope>从你的cdi-api依赖中删除。

编辑:我重新运行了你的测试,并意识到你发生了什么。您正在使用maven-jar-plugin来创建您的 jAR。我假设你想使用 shade 插件来创建一个 uber JAR。这是一个pom.xml您可以使用的更新,它似乎有效(请注意,我还将您的主类更改为 Weld 启动类):

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>de.mycompany</groupId>
<artifactId>weldapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>weldapp</name>
<url>http://maven.apache.org</url>

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
  <dependency>
    <groupId>org.jboss.weld.se</groupId>
    <artifactId>weld-se-core</artifactId>
    <version>3.0.2.Final</version>
  </dependency>
  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
  </dependency>
</dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>org.jboss.weld.environment.se.StartMain</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>
于 2018-01-06T04:02:23.570 回答