IllegalArgumentException:从cmd执行maven项目的Jar时不是文件或目录

时间:2017-09-28 03:26:03

标签: java maven selenium cucumber

我创建了一个黄瓜项目。项目结构可以在下面的截图中看到

我的主要类是util包,RunCukesTest是主类

enter image description here

我尝试通过右键单击项目来创建可执行文件夹

我的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>cucumber-aaa-Maven-Junit</groupId>
    <artifactId>cucumber-aaa-Maven-Junit</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cucumber-aaa-Maven-Junit</name>
    <description>cucumber-aaa-Maven-Junit</description>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.5.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.2.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-core -->
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-core</artifactId>
            <version>1.2.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-junit -->
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.2.5</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>com.vimalselvam</groupId>
            <artifactId>cucumber-extentsreport</artifactId>
            <version>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>com.relevantcodes</groupId>
            <artifactId>extentreports</artifactId>
            <version>2.41.2</version>
        </dependency>

        <dependency>
            <groupId>com.aventstack</groupId>
            <artifactId>extentreports</artifactId>
            <version>3.0.7</version>
            <scope>provided</scope>
        </dependency>


    </dependencies>

    <build>
            <plugins>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>util.RunCukesTest</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
    </build>

</project>

我通过右键单击projet创建了runnable jar - &gt; Export - &gt; Runnable jar - &gt;然后将所需的库复制到生成的Jar

旁边的子文件夹中

见下面的SS

[在此输入图像说明] [2]

我打开了cmd并给了java -jar Maven.jar并收到错误消息&#34; java.lang.IllegalArgumumentException:不是文件或目录:&#34; SS下面

请帮助我解决这个问题。

我也试过过Library - &gt;将所需的库提取到生成的Jar中,同时导出到Jar但没有运气。

enter image description here

2 个答案:

答案 0 :(得分:0)

src / test / ** / won默认是你的jar文件的一部分。请参阅此SO帖子,了解如何创建可执行的test-jar

How can I include test classes into Maven jar and execute them?

答案 1 :(得分:0)

使用这些,

p print(String(data: try! JSONSerialization.data(withJSONObject: object, options: .prettyPrinted), encoding: .utf8 )!)

这个xlsxreader.java

package com;
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Example {
    WebDriver driver = null;
    @Test(dataProvider="getData")
    public void SearchTerm(String browser,String str1, String str2) {
        /*int c = str2.split(str1).length - 1;
        if(c>0)
            System.out.println("Number of times found: "+c);*/
        if(browser.equalsIgnoreCase("chrome")) {
            System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\Drivers\\chromedriver.exe");
            driver = new ChromeDriver();
        }else if(browser.equalsIgnoreCase("Firefox")){
            System.setProperty("webdriver.gecko.driver", "D:\\Selenium\\Drivers\\geckodriver.exe");
            DesiredCapabilities capabilities = DesiredCapabilities.firefox();
            capabilities.setCapability("marionette", true);
            driver = new FirefoxDriver(capabilities);
        }
        int i = 0;
        Pattern p = Pattern.compile(str1);
        Matcher m = p.matcher( str2 );
        while (m.find()) {
            i++;
        }
        System.out.println(i);
    }
    @Test
    public void takeScreenshot() throws IOException {
        File reportFile;
        reportFile =  new File("");;
        while (reportFile.exists());
        File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshot, reportFile);
    }
    @DataProvider(name = "getData", parallel = true )
    public Object[][] getData() throws Exception{
        XlsxReader xls = new XlsxReader("Data");
        int rows = xls.getRowCount("Sheet1");
        int cols = xls.getColumnCount("Sheet1");

        Object[][] data = new Object[rows-1][cols];

        for(int rNum=2;rNum<=rows;rNum++) {
            for(int cNum=0;cNum<cols;cNum++) {
                data[rNum-2][cNum] = xls.getCellData("Sheet1", cNum, rNum);
                System.out.println(xls.getCellData("Sheet1", cNum, rNum));
            }
        }
        return data;
    }
}