无法运行黄瓜jar文件:java.lang.noclassDefFounderror:cucumber / api / cli / main

时间:2017-06-11 12:47:54

标签: java eclipse maven selenium cucumber

我用eclipse创建了黄瓜测试用例的maven项目。在通过maven测试运行项目时,它运行得很好,在运行maven安装时,它会创建包含所有类的maven jar文件。 但是在运行jar文件时我收到错误: java.lang.NoDefFoundError:cucumber/api/cli/Main .

通过eclipse运行项目时没有出现这样的错误。我浏览了某些网站并意识到它与类路径有关。所以我设置了classpath和project目录,如下所示。还为pom.xml添加了清单的类路径。在创建jar之后,我也遇到了同样的错误。

    Please find my 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>com.cucumber</groupId>
        <artifactId>BsMonitor</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>

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

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

        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>info.cukes</groupId>
                <artifactId>cucumber-java</artifactId>
                <version>1.1.5</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>info.cukes</groupId>
                <artifactId>cucumber-picocontainer</artifactId>
                <version>1.1.5</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>info.cukes</groupId>
                <artifactId>cucumber-junit</artifactId>
                <version>1.1.5</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>jdom</groupId>
                <artifactId>jdom</artifactId>
                <version>1.1</version>
            </dependency>
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-server</artifactId>
                <version>2.53.0</version>
            </dependency>
            <dependency>
                <groupId>com.codeborne</groupId>
                <artifactId>phantomjsdriver</artifactId>
                <version>1.2.1</version>
            </dependency>
            <dependency>
                <groupId>org.sonarsource.sonarlint.core</groupId>
                <artifactId>sonarlint-core</artifactId>
                <version>2.2</version>
            </dependency>
            <dependency>
                <groupId>info.cukes</groupId>
                <artifactId>cucumber-core</artifactId>
                <version>1.1.5</version>
            </dependency>
        <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-jvm -->
    <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-jvm -->




        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.17</version>
                    <configuration>
                        <includes>
                            <include>**/*Test.java</include>
                        </includes>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>1.6</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <transformers>
                                    <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>com.cucumber.Base.NewMain</mainClass>
                                    </transformer>
                                </transformers>

                                <filters>
                                    <filter>
                                        <artifact>*:*</artifact>
                                        <excludes>
                                            <exclude>META-INF/*.SF</exclude>
                                            <exclude>META-INF/*.DSA</exclude>
                                            <exclude>META-INF/*.RSA</exclude>
                                        </excludes>
                                    </filter>
                                </filters>
                                <!-- Additional configuration. -->
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>test-jar</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <classpathPrefix>lib/</classpathPrefix>
                             <mainClass>com.cucumber.base.NewMain</mainClass>   
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

    My NewMain method calls the feature file : 
    package com.cucumber.Base;

    import java.io.File;
    import java.io.IOException;

    import cucumber.api.cli.Main;

    public class NewMain {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println("Before caling PhantomJS");

            File f = new File("C:\\Test\\text.txt");
            if (f.exists())

                f.delete();
            try {
                f.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {

                // org.junit.runner.JUnitCore.main("com.cucumber.tests.RunnerTest");

                Main.main(new String[] { "-g", "com.cucumber.tests", "src/test/resource/features.feature" });
            } catch (Throwable e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.out.println(e);
            }

            System.out.println("after caling PhantomJS");
        }

    }

This is my classpath set in the environment variables : [classpath in my system][1]
When I opened the Jar my manifest file had these files in its classpath: 
 [manifest file][2]


  [1]: https://i.stack.imgur.com/Ztewk.png
  [2]: https://i.stack.imgur.com/dit8m.png
Please let me know how i can make this work on running a jar file . ANy help would be great . 

0 个答案:

没有答案