我已经谷歌搜索了几天试图弄清楚如何做到这一点,如果有人这样做,我会非常感谢你的帮助。
我有一个自动化测试项目,我在IntelliJ中创建了一个自动化用户与Web应用程序交互的项目。
我想将自动化测试(使用Selenium和TestNG在Java中创建)放入可执行jar文件中,其他人可以通过双击jar文件来运行。
每次我尝试通过导航到项目结构创建一个jar文件 - >神器 - > + - > Jar - >从具有依赖关系的模块开始,它最终会创建一个声称它的jar,
"Could not find or load the main class <package.MainClass> "
当我尝试使用以下命令运行它时:
java -jar MyProject.jar <Manifest Path>
知道为什么我不断得到这个错误,或者有办法成功地做到这一点?
另外,这是我的pom.xml:
<groupId>TestAutomation</groupId>
<artifactId>TestAutomation</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.test.automation.Executable</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.39.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.40.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
答案 0 :(得分:7)
我终于想到了碰巧碰到这个问题的其他人,这就是我如何创建jar文件并成功运行...
我不得不将我的pom.xml文件更改为以下内容:
<groupId>TestAutomation</groupId>
<artifactId>TestAutomation</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.test.automation.Executable</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.40.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
然后,我不得不调整我的主要方法,不使用任何与TestNG相关的调用。例如,我不能在我的主要方法中使用这样的东西:
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] {WordProfFonts2Set0.class});
testng.addListener(tla);
testng.run();
最后,以下是创建相应jar文件的步骤:
注意:
请务必将IE或Chrome驱动程序添加到项目资源文件夹中,并通过代码文件夹而不是计算机的硬盘驱动器进行调用。例如,执行以下操作:
文件文件=新文件(&#34; src \ test \ resources \ binaries \ IEDriverServer.exe&#34;);
不是这个:
File file = new File
("C:\\Users\\<Username>\\<Proj Name>\\src\\test\\java\\src\\
test\\resources\\binaries\\IEDriverServer.exe");
然后在您的jar保存到计算机上的同一文件夹中创建与其中的驱动程序相同的目录:
src
TestAutomation.jar
2。请确保,如果使用IE,则为所有区域设置保护模式或不设置任何区域(在IE中,转到Internet选项...&gt;安全性(选项卡)&gt;启用保护模式复选框)