I am trying to package my code into a single executable JAR and then run the tests from the JAR. When I run
mvn package
and then
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
I get an error:
Error: Could not find or load main class elevator-1.0-SNAPSHOT.jar
If I run just
java -jar elevator-1.0-SNAPSHOT.jar
I get an error saying no main manifest attribute, in target/elevator-1.0-SNAPSHOT.jar
.
I then tried following the advice in Can't execute jar- file: “no main manifest attribute”
But none of my classes or tests have a main method. I'm using org.junit.runner.JUnitCore
as the main method, to just run the tests.
I tried adding it as the main class in the maven-jar-plugin configuration:
<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>
But got an error stating:
Error: Could not find or load main class org.junit.runner.JUnitCore
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
I also tried creating a TestRunner class that has a main method:
public class TestRunner {
public static void main(String[] args) {
JUnitCore.main("introtojava.hw3.elevator.ElevatorTest", "introtojava.hw3.zoo.animals.AnimalsTest");
}
}
But I received a similar error message.
I tried using the maven-assembly-plugin as suggested in the link above, with the same results.
Completely out of ideas.
My project structure:
├── 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
Relevant bit of my pom.xml file:
<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>