ClassNotFound exception while using the Maven Shade Plugin

为君一笑 提交于 2019-12-23 18:21:56

问题


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.

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>

回答1:


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




回答2:


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.




回答3:


Add following snippet to your plugin. This should help.

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


来源:https://stackoverflow.com/questions/7363398/classnotfound-exception-while-using-the-maven-shade-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!