0

I've seen several similar questions on here but I can't figure out how to apply the answers to my particular situation.

I am trying to run JUnit in a main method in IntelliJ and using Maven. Most of the answers refer to Eclipse and not using Maven so I'm not sure how to properly do it here.

Also, in IntelliJ I can just use the gui to run the Junit tests directly with no problem, but I'm not sure if that is unrelated...

Here is the code in question that is failing (this is still in progress so obviously the first part of the program doesn't do anything too useful):

package validator;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;

public class RunValidator {
    public static void main (String[] args) {
        String testDataPath;

        if (args.length == 1) {
            testDataPath = args[0];
        } else {
            System.out.println("Please enter an argument (only one) for the test path data.");
            System.exit(0);
        }

        Result result = JUnitCore.runClasses(Validator.class);
        System.out.println(result);
    }
}

Here is the error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore
        at validator.RunValidator.main(RunValidator.java:22)
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more

I see in other responses that I should add Junit to the classpath, but I'm not sure how to properly do this in Maven. I have it as a dependency in the pom file, so I'd think that would take care of it? For reference, here is my pom file:

<?xml version="1.0" encoding="UTF-8"?>
<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>validation</groupId>
    <artifactId>StatusCodeValidator</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.0.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>htmlunit-driver</artifactId>
            <version>2.23.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-support</artifactId>
            <version>3.0.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.5.0</version>
                <configuration>
                    <mainClass>com.example.Main</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>validator.RunValidator</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

</project>

I feel like I may very easily be missing something simple, but have been searching for a long time now and can't find the answer.

Thanks!

Michael Burke
  • 113
  • 1
  • 9
  • 1
    how are you running your program. JUnit.jar indeed needs to be on that classpath. – MeBigFatGuy Jan 05 '17 at 20:52
  • Thanks @MeBigFatGuy for the response. I was doing: `mvn clean install` followed by `java -jar target/StatusCodeValidator-1.0-SNAPSHOT.jar TestUrls.csv` – Michael Burke Jan 05 '17 at 21:19
  • @MeBigFatGuy thanks for the comment. After reading your response, doing more and more research still, I was able to solve it by following these directions: http://stackoverflow.com/questions/18414812/how-to-add-my-external-jar-file-to-class-path – Michael Burke Jan 06 '17 at 14:54

1 Answers1

0

This solved my issue: how to add my external jar file to class path

Specifically this section from coding_idiot:

That way it'll create a single-jar of large size and build time will be large everytime you try to build.

I instead prefer adding all jars to a lib folder and including in the classpath (jar's manifest), because of which when we have to make some change or redeploy to the client or some place, we can simply give the small jar (not all the dependencies merged within jar)

      <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.kalindiinfotech.webcrawler.MainGUI</mainClass>
                        <!--                            <mainClass>com.KalindiInfotech.busbookingmaven.form.LoginForm</mainClass>-->
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <phase>install</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Community
  • 1
  • 1
Michael Burke
  • 113
  • 1
  • 9
  • I tried with the snippet above, I'm surely missing something. It is not working. I'm new to Maven too, I tried to rebuild the project (Eclipse) and it's still the same. – hjchin Jul 07 '17 at 06:43
  • Hi @hjchin. Sorry to hear this did not work. I've got a lot more maven experience now so maybe I can help :) Can you provide your entire pom file and what commands you are running? That might help. Be sure to try to do an `mvn clean` as well, or if Eclipse has any kind of "maven reimport" feature, try that. Sometimes that can help solve issues. – Michael Burke Jul 08 '17 at 11:45
  • Hi @Michael, I managed to fix it. I removed the tag from this snippet. I think it limited the usage in scope test. I didn't check further on scope usage though. junit junit 4.12 test – hjchin Jul 09 '17 at 08:41