Question

I have a Maven-based Apache Camel project with the following pom file

<?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>      

When I issue the mvn clean install command, it builds successfully. But when I try to run the generated jar file from the command line using java -jar <generated_jar_filename> it gives me the following error:

    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

I have explicitly added the geronimo-servlet_3.0_spec dependency into my pom file as :

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

Inspite of this, it keeps giving me the NoClassDefFoundError when I try to execute the jar.

Does anybody have any clues?

Was it helpful?

Solution 2

I found the solution!!

I'm using spring-boot within my application with the aim of executing it with the command java -jar <generated_file_name>. Which means my jar needs to be spring-bootable

I missed the following plugin in my pom.xml --> spring-boot-maven-plugin which I added as :

<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>

After adding this, it worked like a charm!!

OTHER TIPS

This is caused by setting your scope to runtime, that means org.apache.geronimo.specs artifact needs to be present on the CLASSPATH while running your JAR. You usually want to use provided or runtime scope when you want to deploy your application to some container (in this case I assume you want Servlet one) and you expect your server to provide this dependency for you - which is not your case now because you are running it by yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top