使用 Maven Shade 插件时出现 ClassNotFound 异常

发布于 2024-12-03 16:03:42 字数 3007 浏览 8 评论 0原文

我正在尝试点击此链接: http://maven.apache.org/plugins/maven-shade- plugin/examples.html

我是 Maven 新手。我觉得尝试遵循这个例子有点不够深入。

我能够让 Quartz Scheduler 与 Spring 一起工作。我希望能够使用 jar 文件从命令行运行它。

这是我使用的类和 pom 文件的列表。

shade maven plugin

编辑:

我能够获得一个阴影 jar 文件。我使用了 mvn clean install

但当我尝试从命令行运行它时,出现以下错误。

C:\Users\SpringExample\target>java -jar SpringExample-1.0-SNA PSHOT-shaded.jar 线程“main”中的异常 java.lang.NoClassDefFoundError: org/sonatype/haven/Ex odusCli 引起原因:java.lang.ClassNotFoundException:org.sonatype.haven.ExodusCli 在 java.net.URLClassLoader$1.run(来源未知) 在 java.security.AccessController.doPrivileged(本机方法) 在 java.net.URLClassLoader.findClass(来源未知) 在 java.lang.ClassLoader.loadClass(来源未知) 在 sun.misc.Launcher$AppClassLoader.loadClass(来源未知) 在 java.lang.ClassLoader.loadClass(来源未知) 找不到主类:org.sonatype.haven.ExodusCli。程序将退出。

EDIT2:

我使用此链接在上面的 pom 中使用了以下内容:

http://seanfreitag.wordpress.com/2011/07/25/create-an-executable-jar-with-dependency-using-maven/

 <project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>1.4</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <manifestEntries>
                    <Main-Class>org.sonatype.haven.ExodusCli</Main-Class>
                    <Build-Number>123</Build-Number>
                  </manifestEntries>
                </transformer>
                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                  <resource>META-INF/spring.handlers</resource>
                </transformer>
                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                  <resource>META-INF/spring.schemas</resource>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

I am trying to follow this link:
http://maven.apache.org/plugins/maven-shade-plugin/examples.html

I am new to Maven. I feel a bit out of depth trying to follow the example.

I am able to get Quartz Scheduler to get working with Spring. I want to be able to run it from commandline using jar file.

Here are the list of classes and pom file I used.

shade maven plugin

EDIT:

I am able to get a shade jar file. I used mvn clean install

but when I try to run it from the command line, I get the following errors.

C:\Users\SpringExample\target>java -jar SpringExample-1.0-SNA
PSHOT-shaded.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/sonatype/haven/Ex
odusCli
Caused by: java.lang.ClassNotFoundException: org.sonatype.haven.ExodusCli
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: org.sonatype.haven.ExodusCli. Program will exit.

EDIT2:

I used the following in the pom above using this link:

http://seanfreitag.wordpress.com/2011/07/25/create-an-executable-jar-with-dependencies-using-maven/

 <project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>1.4</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <manifestEntries>
                    <Main-Class>org.sonatype.haven.ExodusCli</Main-Class>
                    <Build-Number>123</Build-Number>
                  </manifestEntries>
                </transformer>
                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                  <resource>META-INF/spring.handlers</resource>
                </transformer>
                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                  <resource>META-INF/spring.schemas</resource>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ζ澈沫 2024-12-10 16:03:42

我没有使用过 Shade,但我怀疑:

  • 您用于调用 Shade 的 pom 未显示

  • Maven 示例显示如何设置 Main-Class 假设类 org.sonatype.haven.HavenCli 位于正在组装的 jar 中的某个位置

  • 您没有这样的类

  • 您应该更改 org 中的类名称.sonatype.haven.HavenCli 到您想要用作主类的任何内容

I haven't used Shade, but I suspect that:

  • the pom that you are using to invoke Shade is not shown

  • the Maven example showing how to set a Main-Class assumes that the class org.sonatype.haven.HavenCli is somewhere in the jar being assembled

  • you have no such class

  • you should change the class name in your <mainClass>org.sonatype.haven.HavenCli</mainClass> to whatever you want to use as a main class

待天淡蓝洁白时 2024-12-10 16:03:42

更新:您需要在 jar 中的 Manifest.mf 中指定 Main-Class 属性。请参阅示例“Shade 插件,其中将主类添加到 MANIFEST.MF”。

--

是的,您应该将插件代码嵌入到您的 pom 文件中,如下所示

<project>
       <!-- Other tags -->
        ---
    <build>
      <plugins>
        <plugin>
           ---
        </plugin>
      </plugins>
    </build>`
</project>

您可能已将 作为 的直接子项包含在pom 文件。这不起作用。

如果指定了阴影插件配置,则使用 mvn install (或 mvn package)构建 Maven 项目的常用方法将创建阴影 jar。所以,会有两个罐子;原始罐子和超级罐子。

排除:
一般来说,uber jar 会包含 pom.xml 中 dependencies jar 列表中的所有类。 excludes 指定一组不需要位于阴影 jar 中的 jar 文件。如果您仔细查看该示例,您会发现它排除了 junit:junit jar,这意味着 junit 中的类不会出现在您的 uber jar 中。

UPDATE: You need to specify the Main-Class attribute in the Manifest.mf in your jar. See the example "Shade Plugin where a Main-Class is added to the MANIFEST.MF" for that.

--

Yes, you should embed the plugin code to your pom file like the following

<project>
       <!-- Other tags -->
        ---
    <build>
      <plugins>
        <plugin>
           ---
        </plugin>
      </plugins>
    </build>`
</project>

You have probably included the <plugin> as a direct child of <project> in the pom file. It doesn't work.

The usual way of building maven projects with mvn install (or mvn package) will create the shaded jar if the shade plugin configuration are specified. So, there will be two jars; the original jar and the uber jar.

exclude:
Generally, the uber jar will include all the classes in the dependencies jar list in the pom. the excludes specify a set of jar files that need NOT be in the shaded jar. If you have a closer look at the example, it excludes the junit:junit jar, which means that classes in the junit will not be in your uber jar.

夏见 2024-12-10 16:03:42

将以下代码片段添加到您的插件中。这应该有帮助。

 <artifactSet>
    <includes>
        <include>org.sonatype.haven.ExodusCli:*</include>
    </includes>
 </artifactSet>

Add following snippet to your plugin. This should help.

 <artifactSet>
    <includes>
        <include>org.sonatype.haven.ExodusCli:*</include>
    </includes>
 </artifactSet>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文