Question

I've been Googling for days trying to figure out how to do this, if anybody has done this before I would greatly appreciate the help.

I have an automation test project I've created in IntelliJ that automates a user interacting with a Web Application.

I'd like to put that automated test (created in Java, using Selenium and TestNG) into an executable jar file that others can run by double-clicking the jar file.

Every time I attempt to create a jar file by navigating to Project Structure -> Artifact -> + -> Jar -> From modules with dependencies, it ends up creating a jar that claims it,

"Could not find or load the main class <package.MainClass> "

when I attempt to run it with the following command:

java -jar MyProject.jar <Manifest Path>

Any idea why I continually get this error, or have a way to do this successfully?

Also, here is my 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>

Was it helpful?

Solution

I finally figured it out for anyone else who happens to run into this problem, this is how I got the jar file to be created and run successfully...

I had to change my pom.xml file to the following:

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

Then, I had to adjust my main method to not use any TestNG-related calls. For example, I could not use something like this for my main method:

    TestListenerAdapter tla = new TestListenerAdapter();
    TestNG testng = new TestNG();
    testng.setTestClasses(new Class[] {WordProfFonts2Set0.class});
    testng.addListener(tla);
    testng.run();

Finally, here are the steps to get the appropriate jar file created:

  1. Select File > Project Structure... from the top menu
  2. Select "Artifact" on the left menu and click the '+'
  3. Select Jar > From modules with dependencies...
  4. Select your main class using the browse button
  5. Click the radio button next to, "extract to the target jar" and click "OK"
  6. Click the '+' then select "Module Test Output"
  7. In the Available Elements pane to the right, expand the project name and select all the Maven files, then move them to the jar directory being created in the left pane
  8. Click "OK"
  9. Select Build > Build Artifacts... from the top menu
  10. Hover over the jar created and click "Build" under Actions

Notes:

  1. Be sure to add the IE or Chrome driver to your projects resources folder, and call it via the code folder, rather than the computer's hard drive. For example, do this:

    File file = new File("src\test\resources\binaries\IEDriverServer.exe");

Not this:

File file = new File
("C:\\Users\\<Username>\\<Proj Name>\\src\\test\\java\\src\\
  test\\resources\\binaries\\IEDriverServer.exe");

Then create the same directory with the driver in it in the same folder your jar is saved to on your computer:

src
TestAutomation.jar

2 . Be sure, if using IE, that Protected Mode is set for either all zones or none of them (in IE, go to Internet Options... > Security (tab) > Enable Protected Mode check box)

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