使用Maven在IntelliJ中运行Junit,获得java.lang.NoClassDefFoundError:org/Junit/runner/JUnitCore
我在这里看到了几个类似的问题,但我不知道如何将答案应用到我的特定情况 我试图在IntelliJ中的一个主方法中运行JUnit,并使用Maven。大多数答案都是关于Eclipse而不是使用Maven的,所以我不知道如何在这里正确地使用它 此外,在IntelliJ中,我可以直接使用gui运行Junit测试,没有问题,但我不确定这是否无关 下面是正在讨论的失败代码(这仍在进行中,因此程序的第一部分显然没有做任何有用的事情): 以下是错误:使用Maven在IntelliJ中运行Junit,获得java.lang.NoClassDefFoundError:org/Junit/runner/JUnitCore,java,maven,intellij-idea,junit,Java,Maven,Intellij Idea,Junit,我在这里看到了几个类似的问题,但我不知道如何将答案应用到我的特定情况 我试图在IntelliJ中的一个主方法中运行JUnit,并使用Maven。大多数答案都是关于Eclipse而不是使用Maven的,所以我不知道如何在这里正确地使用它 此外,在IntelliJ中,我可以直接使用gui运行Junit测试,没有问题,但我不确定这是否无关 下面是正在讨论的失败代码(这仍在进行中,因此程序的第一部分显然没有做任何有用的事情): 以下是错误: Exception in thread "main" java
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
我在其他响应中看到,我应该将Junit添加到类路径中,但我不确定如何在Maven中正确地实现这一点。我把它作为pom文件中的依赖项,所以我认为这样就可以了?以下是我的pom文件供参考:
<?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>
4.0.0
验证
状态代码验证器
1.0-快照
org.seleniumhq.selenium
硒爪哇
3.0.1
朱尼特
朱尼特
4.8.2
org.seleniumhq.selenium
htmlunit驱动程序
2.23.1
org.seleniumhq.selenium
硒载体
3.0.1
org.apache.maven.plugins
maven编译器插件
3.6.0
1.8
1.8
org.codehaus.mojo
execmaven插件
1.5.0
com.example.Main
org.apache.maven.plugins
maven jar插件
3.0.2
真的
解放党/
validator.RunValidator
1.8
1.8
我觉得我可能很容易错过一些简单的东西,但我已经寻找了很长时间,却找不到答案
谢谢 这解决了我的问题:
具体而言,本节来自“编码”部分:
这样一来,它将创建一个大容量的jar,每次您尝试构建时,构建时间都会很长
相反,我更喜欢将所有jar添加到一个lib文件夹中,并包含在类路径(jar的清单)中,因为当我们必须进行一些更改或重新部署到客户机或某个地方时,我们只需给出一个小jar(而不是jar中合并的所有依赖项)
org.apache.maven.plugins
maven jar插件
2.4
真的
解放党/
com.kalindiinfotech.webcrawler.MainGUI
maven依赖插件
安装
复制依赖项
${project.build.directory}/lib
您是如何运行您的程序的。JUnit.jar确实需要在这个类路径上。感谢@MeBigFatGuy的回复。我在做:mvn clean install
后面是java-jar-target/statuscodevalidater-1.0-SNAPSHOT.jar TestUrls.csv
@MeBigFatGuy谢谢你的评论。在阅读了你的回复,做了越来越多的研究之后,我通过以下方法解决了这个问题:我尝试了上面的片段,我肯定遗漏了一些东西。它不起作用。我也是Maven的新手,我试图重建项目(Eclipse),但它仍然是一样的。很遗憾听到这个不起作用。我现在有了更多的maven经验,所以也许我可以帮你:)你能提供你的整个pom文件和你正在运行的命令吗?这可能会有帮助。一定要尝试进行mvn清理
,或者如果Eclipse有任何类型的“maven重新导入”特性,那么就尝试一下。有时这可以帮助解决问题。嗨,迈克尔,我设法解决了它。我从这个片段中删除了标记。我认为它限制了范围测试的使用。不过,我没有进一步检查范围的使用情况。junit junit 4.12测试
<?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>
<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>