我试图将我的代码打包到单个可执行JAR中,然后从JAR运行测试。当我跑步时
mvn package
然后
java -jar -cp ~/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:~/.m2/repository/junit/junit/4.11/junit-4.11.jar:. elevator-1.0-SNAPSHOT.jar cscie55.hw3.elevator.ElevatorTest
我得到一个错误:
Error: Could not find or load main class elevator-1.0-SNAPSHOT.jar
如果我只是跑步
java -jar elevator-1.0-SNAPSHOT.jar
我收到一个错误,说no main manifest attribute, in target/elevator-1.0-SNAPSHOT.jar
。
然后我尝试遵循Can't execute jar- file: “no main manifest attribute”
中的建议但是我所有的类或测试都没有main方法。我使用org.junit.runner.JUnitCore
作为主要方法,只是运行测试。
我尝试将其添加为maven-jar-plugin配置中的主类:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>org.junit.runner.JUnitCore</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
但是出现错误说明:
Error: Could not find or load main class org.junit.runner.JUnitCore
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
我还尝试创建一个具有主要方法的TestRunner类:
public class TestRunner {
public static void main(String[] args) {
JUnitCore.main("introtojava.hw3.elevator.ElevatorTest", "introtojava.hw3.zoo.animals.AnimalsTest");
}
}
但是我收到了类似的错误消息。
我尝试使用上面链接中建议的maven-assembly-plugin,结果相同。
完全没有想法。
我的项目结构:
├── Projects.iml
├── pom.xml
└── src
├── main
│ └── java
│ └── introtojava
│ └── hw3
│ ├── elevator
│ │ ├── Building.java
│ │ ├── Elevator.java
│ │ ├── ElevatorFullException.java
│ │ ├── Floor.java
│ │ └── Passenger.java
│ └── zoo
│ ├── animals
│ │ ├── Animal.java
│ │ ├── Elephant.java
│ │ ├── Hippopotamus.java
│ │ ├── Rhinoceros.java
│ │ ├── Tiger.java
│ │ └── Zebra.java
│ └── iface
│ ├── Eatable.java
│ ├── Playable.java
│ └── Speakable.java
└── test
└── java
└── introtojava
└── hw3
├── TestRunner.java
├── elevator
│ └── ElevatorTest.java
└── zoo
└── animals
└── AnimalsTest.java
我的pom.xml文件的相关位:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>compile</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>
${basedir}/target/classes/src
</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src</directory>
<includes>
<include>**/*.java</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</build>
</project>