尽管pom.xml中存在显式依赖,但Jar执行会给出NoClassDefFoundError

时间:2014-04-22 14:37:59

标签: java maven pom.xml noclassdeffounderror executable-jar

我有一个基于Maven的Apache Camel项目,其中包含以下pom文件

<?xml version="1.0" encoding="UTF-8"?> <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://maven.apache.org/POM/4.0.0"
     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>

<parent>
    <groupId>com.rmg.maven</groupId>
    <artifactId>rmg-company-pom</artifactId>
    <version>1.2.0-SNAPSHOT</version>
</parent>
<groupId>com.rmg</groupId>
<artifactId>price-finder-camel</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
    <start.class>com.rmg.pricefinder.Application</start.class>
    <spring-boot.version>0.5.0.M7</spring-boot.version>
    <jackson.version>2.3.1</jackson.version>
    <httpclient.version>4.3.2</httpclient.version>
    <pmd.version>3.0.1</pmd.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <compileSource>1.7</compileSource>
    <surefire.version>2.12.3</surefire.version>
    <hystrix.version>1.2.2</hystrix.version>
    <jaxb2.plugin.version>1.6</jaxb2.plugin.version>
    <camel.version>2.13.0</camel.version>
    <cxf.version>3.0.0-milestone2</cxf.version>
    <hibernate.validator.version>5.1.0.Final</hibernate.validator.version>
    <tomcat.jasper.el.version>6.0.39</tomcat.jasper.el.version>        
</properties>

<dependencies>
    <dependency>
        <groupId>com.rmg.maven</groupId>
        <artifactId>rmg-camel-dependencies-pom</artifactId>
        <version>0.1.0-SNAPSHOT</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>com.rmg.maven</groupId>
        <artifactId>rmg-cxf-dependencies-pom</artifactId>
        <version>0.1.0-SNAPSHOT</version>
        <type>pom</type>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring-boot.version}</version>
        <!-- <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jetty</artifactId>
            </exclusion>
        </exclusions> -->
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
        <version>${spring-boot.version}</version>
    </dependency>

    <!-- Additional Spring Dependencies - Spring Data Redis -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-redis</artifactId>
        <version>1.2.1.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.4.2</version>
    </dependency>

    <!-- Camel Components -->
    <!-- Camel Jackson -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jackson</artifactId>
        <version>${camel.version}</version>
    </dependency>
    <!-- Camel HTTP4 -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-http4</artifactId>
        <version>${camel.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>${httpclient.version}</version>
    </dependency>
    <!-- Camel Velocity -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-velocity</artifactId>
        <version>${camel.version}</version>
    </dependency>
    <!-- Camel JAXB -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jaxb</artifactId>
        <version>${camel.version}</version>
    </dependency>
    <!-- Camel Saxon for XSLT 2.0 -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-saxon</artifactId>
        <version>${camel.version}</version>
    </dependency>
    <!-- Camel Quartz2 -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-quartz2</artifactId>
        <version>${camel.version}</version>
    </dependency>
    <!-- Camel CXF -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-cxf</artifactId>
        <version>${camel.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- Apache CXF -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxrs</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-client</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-extension-providers</artifactId>
        <version>${cxf.version}</version>
    </dependency>                     
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-bindings-soap</artifactId>
        <version>${cxf.version}</version>
    </dependency>   
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-features-clustering</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-security-oauth</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-security-cors</artifactId>
        <version>${cxf.version}</version>
    </dependency>                                         
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-ws-security</artifactId>
        <version>${cxf.version}</version>
    </dependency>

    <!-- Hibernate Bean Validator -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>${hibernate.validator.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>jasper-el</artifactId>
        <version>${tomcat.jasper.el.version}</version>
    </dependency>      

    <!-- Jackson JSON -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>${jackson.version}</version>
    </dependency>

    <!-- Hystrix -->
    <dependency>
        <groupId>com.netflix.hystrix</groupId>
        <artifactId>hystrix-core</artifactId>
        <version>${hystrix.version}</version>
    </dependency>
    <dependency>
        <groupId>com.netflix.hystrix</groupId>
        <artifactId>hystrix-metrics-event-stream</artifactId>
        <version>${hystrix.version}</version>
    </dependency>

    <!-- TESTING -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-test-spring</artifactId>
        <version>${camel.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-spring-javaconfig</artifactId>
        <version>${camel.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jolokia</groupId>
        <artifactId>jolokia-spring</artifactId>
        <classifier>plugin</classifier>
        <version>1.2.0</version>
    </dependency>
    <dependency>
        <artifactId>geronimo-servlet_3.0_spec</artifactId>
        <groupId>org.apache.geronimo.specs</groupId>
        <version>1.0</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <configuration>
                <configLocation>src/main/resources/not_packaged_in_archive/rmg-checkstyle.xml</configLocation>
                <propertyExpansion>
                    suppressions.file=src/main/resources/not_packaged_in_archive/rmg-checkstyle-suppressions.xml
                </propertyExpansion>
                <propertyExpansion>
                    suppressions.file=src/main/resources/not_packaged_in_archive/rmg-checkstyle-suppressions.xml
                </propertyExpansion>
            </configuration>
        </plugin>
        <!-- wsdl2java plugin-->
        <plugin>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-codegen-plugin</artifactId>
            <version>${cxf.version}</version>
            <executions>
                <execution>
                    <id>generate-login-client-classes</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <sourceRoot>${basedir}/target/generated-sources/wsdl2java</sourceRoot>
                        <wsdlOptions>
                            <wsdlOption>
                                <wsdl>${basedir}/src/main/resources/META-INF/wsdl/testmbo_login.wsdl</wsdl>
                                <extraargs>
                                    <extraarg>-validate</extraarg>
                                    <extraarg>-client</extraarg>
                                    <extraarg>-verbose</extraarg>
                                    <extraarg>-p</extraarg>
                                    <extraarg>com.bigmachines.security.service</extraarg>
                                    <extraarg>-exsh</extraarg>
                                    <extraarg>true</extraarg>
                                </extraargs>
                            </wsdlOption>
                        </wsdlOptions>
                    </configuration>
                    <goals>
                        <goal>wsdl2java</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <!-- JAXB plugin -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>${jaxb2.plugin.version}</version>
            <executions>
                <execution>
                    <id>generate-source</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <packageName>com.rmg.pricefinder.model</packageName>
                <schemaDirectory>src/main/resources/META-INF/xsd</schemaDirectory>
                <schemaFiles>mailServices.xsd, pips.xsd, weight-bands.xsd, destinations.xsd</schemaFiles>
                <bindingDirectory>src/main/resources/META-INF/jaxb</bindingDirectory>
                <bindingFiles>serializable.xjb</bindingFiles>
            </configuration>
        </plugin>

        <!-- Explicitly run PMD in the build -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pmd-plugin</artifactId>
            <version>${pmd.version}</version>
            <configuration>
                <linkXRef>true</linkXRef>
                <sourceEncoding>${project.build.sourceEncoding}</sourceEncoding>
                <minimumTokens>100</minimumTokens>
                <targetJdk>${compileSource}</targetJdk>
                <excludes>
                    <exclude>**com/bigmachines/security/service/*.java</exclude>
                    <exclude>**com/bigmachines/security/service/runtime/*.java</exclude>
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>check</goal>
                        <goal>cpd-check</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <!-- Surefire configuration to run specified group of tests -->
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${surefire.version}</version>
            <configuration>
                <groups>${tests.group}</groups>
            </configuration>
        </plugin>            
    </plugins>
</build>

<profiles>
    <!-- 'dev' profile - runs only unit & acceptance tests. This is the default profile.
    Gets activated by default or when the value of the system property 'ENVIRONMENT' is 'dev' -->
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <property>
                <name>ENVIRONMENT</name>
                <value>dev</value>
            </property>
        </activation>            
        <properties>
            <tests.group>
                com.rmg.pricefinder.camel.PriceFinderAcceptanceTest,
                com.rmg.pricefinder.camel.PriceFinderUnitTest
            </tests.group>
        </properties>
    </profile>        
    <!-- 'sit' profile - runs integration tests only.
    Gets activated when the value of the system property 'ENVIRONMENT' is 'sit' -->
    <profile>
        <id>sit</id>
        <activation>
            <property>
                <name>ENVIRONMENT</name>
                <value>sit</value>
            </property>
        </activation>            
        <properties>
            <tests.group>com.rmg.pricefinder.camel.PriceFinderIntegrationTest</tests.group>
        </properties>
    </profile>
</profiles>      

当我发出mvn clean install命令时,它会成功构建。但是当我尝试使用java -jar <generated_jar_filename>从命令行运行生成的jar文件时,它给出了以下错误:

    C:\Users\aattarwa>java -jar D:\Users\aattarwa\.m2\repository\com\rmg\price-finder-camel\1.0.0-SNAPSHOT\price-finder-camel-1.0.0-SNAPSHOT.jar
    Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/Servlet

    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
    at java.lang.Class.getMethod0(Unknown Source)
    at java.lang.Class.getMethod(Unknown Source)
    at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
    Caused by: java.lang.ClassNotFoundException: javax.servlet.Servlet
    at java.net.URLClassLoader$1.run(Unknown Source)
    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)
    ... 6 more

我已将geronimo-servlet_3.0_spec依赖项明确添加到我的pom文件中:

<dependency>
        <artifactId>geronimo-servlet_3.0_spec</artifactId>
        <groupId>org.apache.geronimo.specs</groupId>
        <version>1.0</version>
        <scope>runtime</scope>
</dependency>

尽管如此,当我尝试执行jar时,它仍然给我NoClassDefFoundError

有人有线索吗?

2 个答案:

答案 0 :(得分:0)

这是因为将范围设置为runtime,这意味着在运行JAR时需要在CLASSPATH上出现org.apache.geronimo.specs工件。当您想要将应用程序部署到某个容器时(在这种情况下我假设您希望使用Servlet),您通常希望使用providedruntime范围,并且您希望服务器为您提供此依赖关系 - 现在不是你的情况因为你自己运行它。

答案 1 :(得分:0)

我找到了解决方案!!

我在我的应用程序中使用spring-boot,目的是使用命令java -jar <generated_file_name>执行它。这意味着我的jar需要spring-bootable

我错过了我的pom.xml中的以下插件 - &gt;我添加的spring-boot-maven-plugin

<plugin>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-maven-plugin</artifactId>
     <version>1.0.1.RELEASE</version>
     <executions>
          <execution>
              <goals>
                  <goal>repackage</goal>
              </goals>
              <configuration>
                  <mainClass>${start.class}</mainClass>
              </configuration>
          </execution>
      </executions>
      <configuration>
          <mainClass>${start.class}</mainClass>
      </configuration>
  </plugin>

添加之后,它就像魅力一样!

相关问题